Had to learn this lesson the hard way yesterday. I was working on a script that uses relative paths to the folder where the script is.
I want to use the script from a jenkins Job. The job executed the script from different folder than the scripts own folder and suddenly none of the relative paths where found.
Checkout this script doSource.ps1:
#The Path asscociated with the . changes when you run it from another folder Resolve-Path . #Script Root doesn't change Resolve-Path $PSScriptRoot
And what happens when i run it:
PS F:\data\ps-scripts> .\doSource.ps1 Path ---- F:\data\ps-scripts F:\data\ps-scripts PS F:\data\ps-scripts> cd.. PS F:\data> .\ps-scripts\doSource.ps1 Path ---- F:\data F:\data\ps-scripts
So, if your are using relative paths in your script $PSScriptRoot is your friend.