PSScriptRoot in PowerShell: What is it and How to use it?
PowerShell offers a multitude of built-in variables to assist script developers in various tasks. One of these essential variables is $PSScriptRoot. Understanding how PSScriptRoot functions and its significance can significantly enhance your PowerShell scripting experience.
In this article, I will explain what is PSScriptRoot and how you use it in your PowerShell scripts to make it more portable and reliable.
What is $PSScriptRoot?
$PSScriptRoot is an automatic variable in PowerShell that stores the directory path where the currently executing script resides. This variable helps scriptwriters to reference files, modules, or resources located relative to the script’s location, irrespective of where the script itself is run from.
PSScriptRoot variable in PowerShell Script
Let’s take an example scenario where a PowerShell script references other files or modules using relative paths.
You have a PowerShell script (WelcomeScript.ps1) located in a folder along with a file named “welcome.txt”. You want to read the content of welcome.txt from your script.
In the first scenario, I will define a path in the script “C:\rahul”, this is where both the “WelcomeScript.ps1” and the “welcome.txt” reside.
The contents of the script will look something like this:
$filePath = Join-Path -Path C:\rahul -ChildPath "welcome.txt" $content = Get-Content -Path $filePath Write-Output "Content of welcome.txt:" Write-Output $content
Run this script and you’ll be able to read the output of the “welcome.txt” file.
Now, move both the script and welcome.txt file to a different location. I am moving it to “C:\rahul\logs”. After moving both files, re-run the script.
You’ll get an error. This is because the path we mentioned of the “welcome.txt” in the WelcomeScript.ps1 has been changed and we need to change it again in the script too. This is a hassle (doing it again every time the location changes) and that’s why this time we are going to use the $PSScriptRoot variable.
Now the contents of the script will look something like this:
$filePath = Join-Path -Path $PSScriptRoot -ChildPath "welcome.txt" $content = Get-Content -Path $filePath Write-Output "Content of welcome.txt:" Write-Output $content
(The only change is the path of the file, instead of the absolute path, we are using the $PSScriptRoot variable.)
And now run this script. You’ll see it works.
Now, you can move both these files at any location, and you’ll see the script works flawlessly.
PowerShell PSScriptRoot Empty
PowerShell PSScriptRoot serves as a variable designed explicitly to hold the directory path where the currently executing script resides. However, it’s important to note that when used outside of a PowerShell script or if the caller is not a script or command, $PSScriptRoot may indeed appear empty.
Also, PSScriptRoot relies on the context of a running script to determine its value. It won’t be available or populated when executing individual commands or when PowerShell is not running in a script context.
Therefore, if you try to run the contents of the “WelcomeScript.ps1” individually in a PowerShell console, it will output an error that will look something like this:
$filePath = Join-Path -Path $PSScriptRoot -ChildPath "welcome.txt"
Wrapping Up!
Always employ PSScriptRoot when referencing resources related to the script’s location.
As you have seen, without PSScriptRoot, running the script from different locations might lead to failures due to incorrect path resolution.
Moreover, when scripts are properly structured using PSScriptRoot, they become more maintainable. Updates or changes to file locations become hassle-free as the script’s internal references are tied to its own location rather than absolute paths (that might change over time). By utilizing PSScriptRoot, you ensure the script’s portability, reliability and maintainability across various environments.
If you like this post, then follow CenturyBuzz on Facebook and X (Twitter) for more reviews, tips and tutorials.