PDQ.com mobilePDQ.com desktop
Support

Write and Run Your First Windows PowerShell Script

Kris PowellKris Powell
·

System administrators love shortcuts and methods that save time. I think this goes two-fold for the aggravation that comes with managing many computers.

Enter Windows PowerShell...

Though there are many 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.

How do I create a PowerShell script?

PowerShell saves scripts in the ps1 format. Both a file and a folder have been created for our demonstration.

C:\Scripts\My First Script.ps1

Please feel free to use your own custom folder and file names.

First, create the new ps1 file and add the Write-Host cmdlet (cmdlet is another word for command)

Write-Host "Hello, World!"

Save your ps1 file and return to the PowerShell window. In order to run the script, the most common method is by calling 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 4 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.

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.

In order to change the execution policy, we will need to reopen PowerShell as an Administrator (the command will fail otherwise) and run the following command:

Set-ExecutionPolicy RemoteSigned

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!

Next Steps

As this blog series continues, we'll continue to add more helpful tips on how to use PowerShell. In the meantime, you know have the amazing power to create and run your own scripts and cmdlets.

We invite you to check out our list of Windows PowerShell Cmdlets to get familiar with how PowerShell operates.

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!

(CVE-2020-1472) 'ZeroLogon' Vulnerability

The Zerologon Vulnerability Allows Attackers To Completely Take Over Your Domain Controller Without Credentials.

PDQ.com
© 2020 PDQ.com Corporation

Products

  • PDQ Deploy
  • PDQ Inventory
  • PDQ Link
  • Enterprise SL
  • Pricing
  • Downloads
  • Licensing
  • Buy