Skip to content

Guide to writing and running your first PowerShell scripts

Jordan Hammond fun headshot
Jordan Hammond|Updated November 20, 2024
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 the PowerShell scripting language. 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 task automation is becoming mandatory. No better place to start learning PowerShell starting from the basics. 

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 PowerShell cmdlet

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

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

Step 2: Save and try to run the script 

Save your .ps1 script file, and return to the PowerShell window. When running PowerShell scripts, 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. 

Screenshot showing the error message that My First Script.ps1 cannot be loaded.

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, open 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 script 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 Windows PowerShell console:

Screenshot showing that running My First Script.ps1 generates the message "Hello World!"

Congratulations — you just wrote your first PowerShell script!

ConnectIcon CTA

Easily run PowerShell scripts on remote devices

Need to run your awesome PowerShell scripts on remote devices? PDQ Connect can easily execute PowerShell scripts on any managed device with an active internet connection. 

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 PowerShell modules and start with scripts against critical systems, like Azure, VMWare, AWS, or Active Directory. 

Learn PowerShell with the pros

Even the most experienced PowerShell aficionados are constantly learning. Ready to dive into more advanced functions? Check out The PowerShell Podcast to learn more about key people, resources, and modules.

Take your next steps with PowerShell 

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

Now, check out some of the top PowerShell modules and 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