dot source the files from the script directory in both Powershell console and ISE?

1.4k Views Asked by At

I have the following script in directory c:\scripts\.

# . c:\scripts\anotherScript.ps1
function GetScriptRoot { Split-Path $script:MyInvocation.MyCommand.Path }
. "$(GetScriptroot)\anotherScript.ps1"

However, it raises an error in ISE. Is it a way which works in both console and ISE? I am trying not to use the full absolute path.

1

There are 1 best solutions below

1
On BEST ANSWER

The $MyInvocation.MyCommand.Path property is only available in a running script.

To detect whether your are running in the ISE or not you can check for the $psise variable:

if ($psise) {
    "Running in the ISE"
} else {
    "Not running in the ISE"
}

Or look at the $host.Name property:

PS C:\Users\andy> $host
Name             : Windows PowerShell ISE Host

PS C:\Users\andy> $host
Name             : ConsoleHost