DEV Community

taijidude
taijidude

Posted on • Edited on

6 3

$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.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more