Warren Spencer
Warren Spencer

Reputation: 374

Check if powershell running in REPL (Read-eval-print-loop) mode or via "Run With Powershell"

In other words, is there a way to test in a powershell whether we're running having been right-clicked upon and choosing "Run With Powershell" or by opening a powershell interpreter and running something like & myscript.ps1

I have a script which outputs dialog to the user and I've set it up to pause on completion for the user to read what was printed before it disappears (if running via Run With Powershell). However if the script is being run from a REPL interpreter there is no need to pause, as the output will persist in the shell after the script completes

Upvotes: 0

Views: 171

Answers (1)

SmartieMcfly
SmartieMcfly

Reputation: 43

Your answer might be in the $MyInvocation automatic variable. When I right click on a script and choose Run with PowerShell, an if statement is added to the command to check execution policy. You might be able to find a difference between the invocation methods in your environment.

Write-Output "My Invocation Testing"

if ($MyInvocation.Line -like "if((Get-ExecutionPolicy ) -ne 'AllSigned')*") {
    Write-Host -NoNewLine 'Press any key to continue...';
    [void]$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
}

Upvotes: 1

Related Questions