Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 409 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I call another PowerShell script with a relative path?

#1
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.
Reply

#2
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
Reply

#3
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
Reply

#4
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
```
Reply

#5
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 `.`](

[To see links please register here]

) 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"
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through