DEV Community

taijidude
taijidude

Posted on • Updated on

$PSScriptRoot is your friend...

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

So, if your are using relative paths in your script $PSScriptRoot is your friend.

Top comments (1)

Collapse
 
taijidude profile image
taijidude

You are welcome. Glad i could Help.