How do I call another PowerShell script with a relative path?
Asked Answered
L

4

25

I have the following directory tree:

e:\powershell\services\This-Script-Here-Should-Call-Press any key to continue.ps1
e:\powershell\utils\Press any key to continue.ps1

and now I'd like to call a script named "Press any key to continue.ps1" that sits in the "utils"-folder from a script that I have in the "services"-folder. How do I do that? I cannot figure out the relative path.

I tried to do it this way:

"$ '.\..\utils\Press any key to continue.ps1'"

but it did not work.

Louvre answered 11/9, 2011 at 11:47 Comment(1)
Possible duplicate: https://mcmap.net/q/117190/-call-powershell-script-ps1-from-another-ps1-script-inside-powershell-ise/2291 (though this one isn't specific to relative paths)Meli
D
52

Based on what you were doing, the following should work:

& "..\utils\Press any key to continue.ps1"

or

. "..\utils\Press any key to continue.ps1"

(lookup the difference between using & and . and decide which one to use)

This is how I handle such situations ( and slight variation to what @Shay mentioned):

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$utilsDir  = Join-Path -Path $scriptDir -ChildPath ..\utils

& "$utilsDir\Press any key to continue.ps1"
Dedie answered 11/9, 2011 at 14:54 Comment(3)
A link for & and . would have been nice. Not exactly Google friendly search expressions.Gladine
In case anyone else wants to understand the &, it's a shortcut for "Invoke-Expression" - see technet.microsoft.com/en-us/library/ee176880.aspxSop
for those who are still having issues trying to find the difference between . and & look at the following blog: rkeithhill.wordpress.com/2007/11/24/… TL;DR "." calls script and runs in the current scope "&" calls script and runs in a different child scope and is thrown awayNessy
F
12

Put the following function in the calling script to get its directory path and join the utils path along with the script name:

# create this function in the calling script
function Get-ScriptDirectory { Split-Path $MyInvocation.ScriptName }

# generate the path to the script in the utils directory:
$script = Join-Path (Get-ScriptDirectory) 'utils\Press any key to continue.ps1'

# execute the script
& $script 
Foah answered 11/9, 2011 at 12:5 Comment(4)
there are two problems with this solution: 1st - this is not what I'm trying to achieve becasue the "ulits" directory is not a subdirectory of "services" and I must leave "services" and then enter "utils" to call the right script. 2nd - ScriptName-Property is empty. I've edited the question so it is clearer.Louvre
It really depends on the console working directory (type pwd in your console). If you want to use relative paths you need to cd into the source script directory first and then use ..\folder\script.ps1. Are you sure that Get-ScriptDirectory doesn't return the parent folder of the calling script?Foah
my bad. I must have overseen something the Get-ScriptDirectory function works indeed.Louvre
Using Get-ScriptDirectory willy nilly is likely not to return what you think it should; it all depends how you call it. I wrote extensively on this specific point in my answer to How can I find the source path of an executing script? and then expanded it into an article on Simple-Talk.com: Further Down the Rabbit Hole: PowerShell Modules and Encapsulation.Kokaras
C
7

To get the current script path $PSScriptRoot variable can be used. for example Following is the structure: solution\mainscript.ps1 solution\secondscriptfolder\secondscript.ps1

#mainscript.ps1

$Second = Join-Path $PSScriptRoot '\secondscriptfolder\secondscript.ps1'

$Second
Croner answered 1/6, 2021 at 11:58 Comment(1)
Thanks - this is defo the most direct and simplest solution 👍Looney
A
1

Thank you very informative. I had similar issue, I could get it work like this, to call other scripts from parentfolder.

$CommandPath = (Get-Location).Path | Split-Path -Parent; $script = "$CommandPath\Logins\Login_SSI_SST_exch2010_DKSUND_Exchange365.ps1"; & $script
Arthritis answered 16/3, 2018 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.