Guide to writing and running your first PowerShell scripts
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!"
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.
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:
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:
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.