Powershell – How to Create and Run a Script

As you automate your Windows operating system with PowerShell 2, it helps to know how to create scripts that you may be able to loop and use more than once. The steps to create a script follow:

  1. Create the script in a plain text editor such as Notepad and save with a .PS1 file extension (for example, myscript.ps1).
  2. Run the script by entering the full path to the script (C:\scripts\myscript.ps1), or if it’s in the current directory, prefix it with a period followed by a backslash (.\myscript.ps1).
  3. If the path to the script contains a space, enclose the full path in quotation marks and prefix the entire thing with an ampersand (for example:  &"C:\my scripts\myscript.ps1").

Unless you’re running a signed script, make sure you to set the correct execution policy using Set-ExecutionPolicy. To allow local scripts to run unsigned, run:

Set-ExecutionPolicy RemoteSigned