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.

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, maybe I had to use full path in ISE. How to detect the script is running in ISE/Console?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.