$PSScriptRoot is your friend...
taijidude

taijidude @taijidude

About: Java Developer. Love to read and learn. Hobbies: - tabletop rpgs - boardgames - videogames - cooking - kettlebell

Location:
Hannover, Germany
Joined:
Jul 22, 2020

$PSScriptRoot is your friend...

Publish Date: May 7 '22
11 2

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.

Comments 2 total

  • taijidude
    taijidudeMay 15, 2022

    You are welcome. Glad i could Help.

Add comment