$PSScriptRoot
noun
say it: “PEE-ESS SKRIPT ROOT” /ˌpiˌɛs ˈskrɪpt ˈrut/
A PowerShell variable that holds the folder a running script is saved in, so the script can find files kept beside it.
What is $PSScriptRoot?
$PSScriptRoot is the automatic variable that holds the folder a running script is saved in. A script runs in the folder you're in, so a plain todo.txt means your folder; "$PSScriptRoot\todo.txt" always means the list beside the script.
# morning.ps1, saved in C:\Users\Sam\Scripts
Get-Content -Path "$PSScriptRoot\todo.txt"
# Pay the phone bill
# Finish the history essay
# Buy coffee beans
In a sentence
- “With $PSScriptRoot, the script reads the file beside it no matter where you run it from.”
- “Sam put $PSScriptRoot in front of todo.txt, and his to-do list showed up from any folder.”
Level: Apprentice · More PowerShell terms at this level
Updated