Reputation: 374
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
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