I would like to call script B from script A without specifying the complete path. I've tried with
.\scriptb.ps1
but that doesn't work. Do I really have to specify the entire path?
(sorry for this quite basic question, but google couldn't help me!)
2 or later you can do cd - to navigate to your previous directory. cd is the alias for Set-Location . Adding paramerter + or - goes forward or backward through your location history. +1 This is the best answer for modern users.
$PSScriptRootContains the full path of the executing script's parent directory. In PowerShell 2.0, this variable is valid only in script modules ( . psm1 ). Beginning in PowerShell 3.0, it's valid in all scripts.
You don't need Start-Process. PowerShell scripts can run other scripts. Just put the command that runs the second script as a command in the first script (the same way as you would type it on the PowerShell command line).
i use the powershell variable which has an easy name to remember what is it for.
$PSScriptRoot\scriptb.ps1
it is possible using $MyInvocation
as follows:
$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent $scriptPath = Join-Path $executingScriptDirectory "scriptb.ps1" Invoke-Expression ".\$scriptPath"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With