roy roy - 3 months ago 22
PowerShell Question

Powershell relative path to repo file

I am trying to refer relative path to file which is in the same repo as powershell script.

I have following dir structure.

repo/
├── files
│   └── scripts.sh
│  
├── powershell_scripts
└── my_powershell.ps1


in
my_powershell.ps1
I want to refer
scripts.sh
. I tried to use
$PSScriptRoot
in the powershell script but it didn't work the way I want.

I want
my_powershell.ps1
to pickup
scripts.sh
with directory structure.

anyone knows how do this ?

Thanks

Answer

Ok, so in v3 or higher this is simple. If you want the path to the "Repo" folder you could simply refer to the parent of the current folder such as:

$Repo = Resolve-Path "$PSScriptRoot\.."

This will get you the folder object of Repo. From there you could get to your script folder.

Or to directly refer to the scripts.sh file you could do:

Resolve-Path "$PSScriptRoot\..\files\scripts.sh"
Comments