top of page

How to Write a Basic PowerShell Script

  • Writer: Abisai Hernandez
    Abisai Hernandez
  • Apr 28
  • 3 min read

PowerShell is more than just a fancy command line. It's your magic wand for automating tasks, configuring systems, and turning repetitive processes into smooth workflows. If you've ever found yourself doing the same clicks over and over again ("click here, open that, check this..."), now's the time to let PowerShell do the heavy lifting.


This guide is for you—whether you're an IT professional, a system administrator, or a tech enthusiast looking to master the art of scripting on Windows. We’ll build a strong foundation step by step: understanding what PowerShell is, how to set it up, writing and running your first script, and how to keep growing.

What is PowerShell and Why Use It?


PowerShell is an object-based command-line shell and scripting language, designed specifically for system management. Created by Microsoft, its strength lies in the fact that it doesn't just work with plain text (like CMD), but with full-on system objects.


Why should you use PowerShell?

  • Stress-free automation: From creating users to moving files, everything can be automated.

  • Full system control: Access logs, services, processes, and more.

  • Scalability: Works on a single PC or across hundreds of machines.

  • Modern tool compatibility: Seamlessly integrates with Azure, Active Directory, Microsoft 365, and beyond.



Screenshot of a PowerShell script simulating system maintenance tasks like listing active services, cleaning temp files, analyzing logs, checking disk space, and scanning for updates.


Setting Up PowerShell on Your System


Accessing PowerShell in Windows

PowerShell comes pre-installed on most modern versions of Windows. To open it:

  • Press Win + X and select "Windows PowerShell" or "Windows Terminal".

  • For a more visual scripting interface, search for PowerShell ISE from the Start menu.


    💡 Pro Tip: Use the new Windows Terminal to combine PowerShell, CMD, and WSL into a single tabbed window.


Enabling Execution Policies

For security reasons, Windows restricts script execution. To allow it:

  1. Open PowerShell as Administrator.

  2. Run:

Set-ExecutionPolicy RemoteSigned
  1. When prompted, confirm with Y


This allows local unsigned scripts to run while protecting against untrusted remote ones.


Recommended Editors

While you can use Notepad, Visual Studio Code (VS Code) is the best choice. Install the official PowerShell extension and get:

  • Autocompletion

  • Step-by-step debugging

  • Real-time error detection



PowerShell Syntax and Command Overview


Cmdlets

PowerShell uses commands called cmdlets, written in Verb-Noun format. Common examples:

Get-Process       # Displays active processes
Set-Date          # Sets system date
Restart-Computer  # Restarts the computer

Parameters

Modify cmdlet behavior with parameters:

Get-Service -Name "wuauserv"

Basic Syntax

  • Variables: Declared using $Example: $name = "User"

  • Comments: Use #Example: # This displays active processes

  • Pipelines: Chain commands using |Example:

Get-Process | Where-Object {$_.CPU -gt 100}


Write Your Basic PowerShell Script (Your very first!)


Let’s Get to It

We’ll write a basic powershell script that greets the user and displays the current date and time. A great way to get comfortable with the syntax.

# My first PowerShell script
$user = "User"
Write-Host "Hello, $user!"
Write-Host "The current date and time is: $(Get-Date)"

Save this as Greeting.ps1.


Script Structure

  • Initial comments: Use # to indicate a comment.

  • Variables: Declared with the $ symbol.

  • Simple commands: Write-Host prints to screen, Get-Date retrieves current time.



Running and Testing the PowerShell Script


Step-by-Step

  1. Open PowerShell.

  2. Navigate to the folder where you saved the script:

cd C:\Scripts
  1. Run the script:

.\Greeting.ps1

Debugging Tips

  • Use Write-Output or Write-Host to display values during execution.

  • Check for syntax errors—VS Code highlights them automatically.

  • Make sure you have the right permissions.


Digital illustration representing scripting and automation with PowerShell, showing a desktop monitor and smartphone surrounded by icons of code, cloud download, and gears — symbolizing system tasks, development, and script execution

Best Practices for PowerShell Scripting


As your scripts grow in complexity, following good practices becomes crucial for keeping your code clean, secure, and maintainable. Use clear, descriptive names for variables and functions—it’ll save you headaches down the line (especially when revisiting that script six months later and wondering what it does!). Document your code with helpful comments explaining why you're doing something, not just what you're doing.


Error handling is key: a script that works is nice, but one that handles failure gracefully is gold. Use try/catch blocks to prevent unexpected crashes. On the security side, never store plain-text passwords. Instead, use Get-Credential or modules like SecretManagement to manage sensitive info. If you plan to share or distribute scripts, consider digitally signing them to ensure integrity.





What’s Next: Leveling Up Your PowerShell Skills


Once you’ve mastered the basics, PowerShell becomes a daily powerhouse. The next step is diving into more advanced tasks—automating account management, generating system reports, scheduling backups, or even deploying apps. You can also integrate PowerShell with platforms like Microsoft 365, Azure, or Active Directory for enterprise-scale automation.

Young programmer with wireless earbuds typing code on a laptop, with a holographic projection of JavaScript commands related to React scripts displayed in front of him, in a modern office setting.

To keep learning, explore quality resources. Microsoft’s official docs are a great starting point. Online platforms like Udemy, Pluralsight, or LinkedIn Learning offer full learning paths. And the PowerShell community is super active—forums like Stack Overflow, Reddit, and TechNet are full of ideas, solutions, and tips. The key is to keep practicing, experiment often, and don’t fear mistakes. Every script you write gets you one step closer to becoming a true scripting pro.






bottom of page