How can I stop Powershell by pressing a key. I have a loop, which should be stopped, when I press "q". How to do that.
I have found the following code, but didn't work
while($true) {
Write-Host "Working" -BackgroundColor Green
if($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyup,NoEcho").Character)) {
Write-Host "Exiting now, don't try to stop me...." -BackgroundColor DarkRed
break;
}
}
Anyone got an idea?
the same code works, but not in PowerShell ISE but in the normal PowerShell console. You guys have to save it if you are scripting and then execute via Powershell console.