26

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.

1

4 Answers 4

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"
Sign up to request clarification or add additional context in comments.

3 Comments

A link for & and . would have been nice. Not exactly Google friendly search expressions.
In case anyone else wants to understand the &, it's a shortcut for "Invoke-Expression" - see technet.microsoft.com/en-us/library/ee176880.aspx
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 away
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 

4 Comments

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.
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?
my bad. I must have overseen something the Get-ScriptDirectory function works indeed.
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.
9

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

1 Comment

Thanks - this is defo the most direct and simplest solution 👍
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.