powershelldelaykeypresspowershell-v5.1powershell-v6.0

Ignore CTRL+C in delay - powershell


Whenever I set a delay in powershell using Start-Sleep, e.g:

Start-Sleep 10

then it does not ignore CTRLC. I mean when I hit that key stroke the delay quits. How can I ignore it in delays?


Solution

  • You can temporarily set [Console]::TreatControlCAsInput to $true:

    [Console]::TreatControlCAsInput = $true
    Start-Sleep 10  # Ctrl-C will now not abort this sleep.
    [Console]::TreatControlCAsInput = $false