2

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.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
ca9163d9
  • 27,283
  • 64
  • 210
  • 413

1 Answers1

2

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
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124