How do I call another PowerShell script with a relative path? - Printable Version +- 0Day Forums (https://0day.red) +-- Forum: Coding (https://0day.red/Forum-Coding) +--- Forum: PowerShell & .ps1 (https://0day.red/Forum-PowerShell-ps1) +--- Thread: How do I call another PowerShell script with a relative path? (/Thread-How-do-I-call-another-PowerShell-script-with-a-relative-path) |
How do I call another PowerShell script with a relative path? - autopolyploid274684 - 07-21-2023 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. RE: How do I call another PowerShell script with a relative path? - alysakezp - 07-21-2023 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 RE: How do I call another PowerShell script with a relative path? - schwa891728 - 07-21-2023 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 RE: How do I call another PowerShell script with a relative path? - babylonian870 - 07-21-2023 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 ``` RE: How do I call another PowerShell script with a relative path? - daly425 - 07-21-2023 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" |