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 Windows PowerShell.
Though there’s a wide variety of great scripting languages out there, it doesn't get much better than Windows PowerShell. If you're using a recent version of Microsoft Windows, you've probably already got a version of it installed. Here’s how to use it to create shortcuts and make your life as a systems admin easier.
PowerShell saves scripts in the ps1 format. Feel free to use your own custom folder and file names. For our demonstration, both a file and a folder have been created:
Step 1: Create the new ps1 file and add the Write-Host cmdlet (cmdlet is another word for command)
Go ahead and try that command. You should get an error that says scripts have been disabled on your system. This is for security reasons.
In order to prevent malicious scripts from running on your system, PowerShell enforces an execution policy. There are four execution policies you can use:
|Restricted||Scripts won’t run. Period. (Default setting)|
|RemoteSigned||Locally-created scripts will run. Scripts that were created on another machine will not run unless they are signed by a trusted publisher.|
|AllSigned||Scripts will only run if signed by a trusted publisher (including locally-created scripts).|
|Unrestricted||All scripts will run regardless of who created them and whether or not they are signed.|
Step 3: In order to use our newly created script, we will have to modify our execution policy to allow our example script to run. Since we have not digitally signed our new script, our options for setting the execution policy are left to “RemoteSigned” and “Unrestricted.” We are going to change it to RemoteSigned.
Step 4: To change the execution policy, reopen PowerShell as an Administrator (the command will fail otherwise) and run the following command:
Step 5: The Set-ExecutionPolicy cmdlet will ask to verify that you really want to change the execution policy. Go ahead and select Y for yes, then go ahead and close and reopen your PowerShell window.
After restarting the PowerShell window, try running your .ps1 script again.
It should write back, "Hello, World!" to the window:
Congratulations, you just wrote your first PowerShell script!
As this blog series continues, we'll continue to add more helpful tips on how to use PowerShell to expedite more of your work processes. In the meantime, you now have the amazing power to create and run your own scripts and cmdlets.
Kris was an employee at PDQ.