Putting the following lines at the end of a PowerShell script has the following functionality:
if($Host.Name -eq "ConsoleHost")
{
Write-Host "Press any key to continue..." -NoNewline
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null
}
- If I run the script by right clicking on it, going to "Run with PowerShell," then the console window stays open for me until I press any key to continue.
- If I already have a PowerShell window open and I run the script from within this window, then although the "Press any key to continue" text still appears, I don't actually have to press anything to continue; it does this on its own. Hence, I can immediately run something else afterward.
I like this behavior, but I would prefer to limit it to a specific subset of keys (or even just a single key, such as the enter key), if possible. Using read-host doesn't work then I still have to enter input even when running from another PowerShell window; the goal is to simply stop the window from closing when I run it from the context menu. I shouldn't have to input anything when running from an already-existing PowerShell console.
Basically, I like the functionality of the code I have above, but I want to limit the keys it applies to.
Is it possible to achieve this? If so, how?
No comments:
Post a Comment