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

t3chb0t picture t3chb0t · Sep 11, 2011 · Viewed 80.9k times · Source

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.

Answer

manojlds picture manojlds · Sep 11, 2011

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"