0

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

0

1 Answer 1

1

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

Comments

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.