PowerShell: Create a Script

By Xah Lee. Date: . Last updated: .

Create a PowerShell Script

You can put commonly used commands into a file, save the file, then you can run it as a script.

For example, create a file with the following content:

# filename: test.ps1

Save this file for example at ~/test.ps1

Run Script

Execution Policy Error

If this is your first time running a script, you'll get a error like this:

File C:\Users\xah\PowerShell scripts\xx.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing"
for more details.

This is because PowerShell has a execution policy for security purposes. By default, it does not allow you to run unsigned scripts. To make it run unsigned scripts you created on your machine, Run the following to set a different policy:

# make local script run but require remote script to be signed
Set-ExecutionPolicy RemoteSigned

PowerShell: Profile and Script