24

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.

Tim Penner
  • 3,551
  • 21
  • 36
t3chb0t
  • 16,340
  • 13
  • 78
  • 118
  • Possible duplicate: http://stackoverflow.com/q/6816450/2291 (though this one isn't specific to relative paths) – Jon Adams Mar 25 '13 at 19:18

4 Answers4

51

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"
ruffin
  • 16,507
  • 9
  • 88
  • 138
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 61
    A link for & and . would have been nice. Not exactly Google friendly search expressions. – Christopher Painter May 14 '13 at 17:43
  • 7
    In case anyone else wants to understand the &, it's a shortcut for "Invoke-Expression" - see http://technet.microsoft.com/en-us/library/ee176880.aspx – Richard J Foster Aug 05 '13 at 18:41
  • 29
    for those who are still having issues trying to find the difference between . and & look at the following blog: http://rkeithhill.wordpress.com/2007/11/24/effective-powershell-item-10-understanding-powershell-parsing-modes/ TL;DR "." calls script and runs in the current scope "&" calls script and runs in a different child scope and is thrown away – stanleykylee Apr 02 '14 at 18:20
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 
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • 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. – t3chb0t Sep 11 '11 at 13:35
  • 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? – Shay Levy Sep 11 '11 at 15:45
  • my bad. I must have overseen something the Get-ScriptDirectory function works indeed. – t3chb0t Sep 11 '11 at 16:26
  • 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?](http://stackoverflow.com/questions/801967/how-can-i-find-the-source-path-of-an-executing-script/6985381#6985381) and then expanded it into an article on Simple-Talk.com: [Further Down the Rabbit Hole: PowerShell Modules and Encapsulation](http://www.simple-talk.com/dotnet/.net-tools/further-down-the-rabbit-hole-powershell-modules-and-encapsulation/). – Michael Sorens Sep 12 '11 at 16:44
5

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
Captain_Levi
  • 87
  • 1
  • 5
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
Syed Waqas Bukhary
  • 5,130
  • 5
  • 47
  • 59