Skip to content

Instantly share code, notes, and snippets.

@pcrockett-pathway
Created May 9, 2019 08:10
Show Gist options
  • Select an option

  • Save pcrockett-pathway/4ec3ba0a716c6b677caad003b7f4b4be to your computer and use it in GitHub Desktop.

Select an option

Save pcrockett-pathway/4ec3ba0a716c6b677caad003b7f4b4be to your computer and use it in GitHub Desktop.
Example of a PowerShell loop that a user can "escape" with a keystroke
[CmdletBinding()]
param(
[Parameter()]
[int]$PollFrequencySeconds = 2
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version 5.0
$stopKey = "Q"
Write-Host "Press $stopKey to stop."
function userWantsToStop() {
while ([Console]::KeyAvailable) {
if ([Console]::ReadKey($true).Key -eq $stopKey) {
return $true
}
}
return $false
}
while (!(userWantsToStop)) {
# Do work here
Start-Sleep -Seconds $PollFrequencySeconds
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment