Skip to main content
PDQ.com mobilePDQ.com desktop
Support
Try Now

Write and Run Your First Windows PowerShell Script

Kris PowellKris Powell
·

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.

How do I create a PowerShell script?

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: 

C:\Scripts\My First Script.ps1

Step 1: Create the new ps1 file and add the Write-Host cmdlet (cmdlet is another word for command)

Write-Host "Hello, World!"

Step 2: 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 Code)

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

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:

RestrictedScripts 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.
AllSignedScripts will only run if signed by a trusted publisher (including locally-created scripts).
UnrestrictedAll 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:

Set-ExecutionPolicy RemoteSigned

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

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

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

Congratulations, you just wrote your first PowerShell script!

Take your next steps with PowerShell

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.

You can also check out our list of Windows PowerShell Cmdlets to get familiar with PowerShell functions and how to use them. 

Ready to put the PowerShell Scanner through its paces?

Take our 14-day Free Trial. Feel the power of the PowerShell scanner.
Start a Trial

Don't miss the next post!

How to Block the Windows 11 Upgrade

Windows 11 is here, but that doesn't mean you have to use it. Discover several different ways to block Windows 11 from installing on your computers.

windows11
PDQ.com
© 2021 PDQ.com Corporation
Products
  • PDQ Deploy ®
  • PDQ Inventory ®
  • Pricing
  • Downloads
  • Licensing
  • Buy