Creating PowerShell Scripts
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.
# test script Get-ChildItem get-date
Save this file for example at
PowerShell File Name Extension
PowerShell file name should end in
To run a script, you can cd to the dir and start the script with
# run a PowerShell script cd ~/pwsh_scripts ./x.ps1
./ means the current directory.
PowerShell requires you to have that to prevent running things found in the current dir by mistake.
You can also call a script by its full path.
# call a script with the Invocation Operator & & " ~/pwsh_scripts/x.ps1"
You need the call operator
&, because your path is a string.
If you don't use the operator, then PowerShell will simply evaluate that string as a expression, and the result is the same string.
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
You can add your script dir to the environment variable: $PATH, so you don't have to cd to the dir first to call it.
see PowerShell: Set Environment Variable