PowerShell Running Scripts
A script is a file that holds PowerShell commands to run together. Running a script runs all its commands in order.
Create and run a one-line script
Set-Content writes the script file first. Then & runs that file.
Set-Content -Path (Join-Path $PSScriptRoot "hello.ps1") -Value 'Write-Output "Hello from a script"'
& (Join-Path $PSScriptRoot "hello.ps1")Run a two-line script
A script can hold more than one command. Its commands run from top to bottom.
Set-Content -Path (Join-Path $PSScriptRoot "two.ps1") -Value 'Write-Output "First line"; Write-Output "Second line"'
& (Join-Path $PSScriptRoot "two.ps1")Run a script that prints a version
A script can also print a value from a built-in command. This script prints a message and a version.
Set-Content -Path (Join-Path $PSScriptRoot "version.ps1") -Value 'Write-Output "Script running"; $PSVersionTable.PSVersion.ToString()'
& (Join-Path $PSScriptRoot "version.ps1")Example
Set-Content -Path (Join-Path $PSScriptRoot "steps.ps1") -Value 'Write-Output "Step one"; Write-Output "Step two"; Get-Date -Date "2026-10-28" -Format "yyyy-MM-dd"'
& (Join-Path $PSScriptRoot "steps.ps1")Key points
- Script: a file that holds commands to run together.
- A script file name ends with .ps1.
- Set-Content writes text into a file.
- $PSScriptRoot is the folder that holds the script now running.
- Join-Path joins a folder and a file name into one path.
- The & sign tells PowerShell to run the file that follows it.
In this example, the first line writes a three-command script file, and the second line runs that file, so all three commands print in order.
Note
A script runs as a whole, so one run can print many results.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What file ending does a PowerShell script use?