Guide to writing and running your first PowerShell scripts

Jordan Hammond fun headshot
Jordan Hammond|Updated November 9, 2023
Illustration of block with Powershell logo
Illustration of block with Powershell logo

System administrators love a good shortcut, especially when it comes to alleviating the aggravation that comes with managing computers, solving user issues, and putting out fires around the office. Enter PowerShell

Though there’s a wide variety of great scripting languages out there, it doesn't get much better than PowerShell. And now, it can run across platforms, making it a useful tool no matter the environment you find yourself working in. Sysadmins have too much on their plates, so automating whatever tasks you can is becoming mandatory. No better place to start learning than learning the basics of PowerShell. 

PowerShell saves scripts in the .PS1 format. Feel free to use your own custom folder and file names. For our demonstration, we created both a file and a folder:

C:\Scripts\My First Script.ps1

Step 1: Create a new file and add a cmdlet  

Create the new .PS1 file, and add the Write-Host cmdlet (cmdlet is another word for command). 

Write-Host "Hello, World!"
Write-Host "Hello, World" PowerShell script

Step 2: Save and try to run the script 

Save your .PS1 file, and return to the PowerShell window. To run the script, the most common method is to call it in the PowerShell terminal. (You can also use the PowerShell ISE or VS CodeVS Code.) 

& "C:\Scripts\My First Script.ps1"

Go ahead and try that command. You should get an error that says scripts are disabled on your system. This is for security reasons. 

"My First Script.ps1"

Step 3: Modify the execution policy

In order to prevent malicious scripts from running on your system, PowerShell enforces an execution policy. To use our newly created script, we have to modify our execution policy to allow our PowerShell example script to run. There are four execution policies

RestrictedScripts won’t run. Period. (Default setting.)
RemoteSignedLocally created scripts run. Scripts that were created on another machine won’t run unless they are signed by a trusted publisher.
AllSignedScripts (including locally created scripts) only run if signed by a trusted publisher.
UnrestrictedAll scripts run regardless of who created them and whether they’re signed.

Since we have not digitally signed our new PowerShell example script, our options for the execution policy are limited to RemoteSigned and Unrestricted. We are going to change it to RemoteSigned. 

To change the execution policy, reopen PowerShell as an administrator (the command fails otherwise), and run the following command: 

Set-ExecutionPolicy RemoteSigned

The Set-ExecutionPolicy cmdlet asks to verify that you really want to change the execution policy. Go ahead and select Y for yes, then close and reopen your PowerShell window. 

Step 4: Run your script 

After restarting the PowerShell window, try running your .PS1 script again. 

& "C:\Scripts\My First Script.ps1"

It should write back, "Hello, World!" to the window:

Running your first powershell script

Congratulations — you just wrote your first PowerShell script!

PowerShell examples for beginners 

When looking for ideas on what you can do with beginner scripts, just think about things you need to do manually and see if can grab the information. One good quick win is checking the ACL of file share: 

Get-Acl "\\fileshare\folder" | Select-Object -ExpandProperty Access

Or try clearing out a temp folder that is taking up space with unneeded documents: 

Get-ChildItem C:\temp | Remove-Item

Or test if a registry key is on a machine:

Get-ItemProperty -Path "HKLM:\SOFTWARE\Admin Arsenal\PDQ Deploy"

Those are all quick wins that you can grab out of the box. The entire world opens once you dive into modules and start with scripts against critical systems, like Azure, VMWare, AWS, or Active Directory. 

Take your next steps with PowerShell 

You now have the amazing power to create and run your own scripts and cmdlets. 

Now, check out our list of PowerShell cmdlets to get familiar with PowerShell functions and how to use them. 

Jordan Hammond fun headshot
Jordan Hammond

Jordan had spent his life wondering why tasks he didn’t like to do had no options to complete themselves. Eventually he had to make that happen on his own. It turned out that he enjoyed making tasks complete themselves, and PDQ thought that is something he should talk about on the internet.

Related articles