Ctrl+C can't be intercepted by PowerShell for shutdown #2661

Closed
opened 2026-01-30 23:01:38 +00:00 by claunia · 8 comments
Owner

Originally created by @tillig on GitHub (Jul 9, 2019).

Originally assigned to: @leonMSFT on GitHub.

Environment

Windows build number: 10.0.18362.175
Windows Terminal version: 0.2.1831.0
PowerShell Core 6.2.1
PowerShell 5.1

I'm on a Microsoft Surface Book. There is no Pause key and no Break key. Ctrl+Break is not an option for stopping a program unless I plug in an external keyboard or do some sort of key remapping specifically for this.

Steps to reproduce

Create a script in PowerShell that intercepts Ctrl+C so it can gracefully shut down. Run that script and try to use Ctrl+C. The script can't intercept it.

It behaves the same in PowerShell Core and in PowerShell.

[Console]::TreatControlCAsInput = $True
Start-Sleep -Seconds 1
$Host.UI.RawUI.FlushInputBuffer()
$exiting = $false
while ((-Not $exiting)) {
    Write-Host "Sleeping..."
    Start-Sleep -Seconds 1
    while ($Host.UI.RawUI.KeyAvailable -and ($Key = $Host.UI.RawUI.ReadKey("AllowCtrlC,NoEcho,IncludeKeyUp"))) {
        If ([Int]$Key.Character -eq 3) {
            Write-Warning "Got CTRL-C!"
            $exiting = $true
        }

        $Host.UI.RawUI.FlushInputBuffer()
    }
}
Write-Host "Done."
[Console]::TreatControlCAsInput = $False

Expected behavior

I should be able to hit Ctrl+C to stop the program. In a standard PowerShell terminal (not hosted in the new Microsoft Terminal) this works fine.

Actual behavior

The program pauses, ostensibly to allow copy/paste behavior to work, and if I hit Enter it continues running. There is no way to capture Ctrl+C.

Originally created by @tillig on GitHub (Jul 9, 2019). Originally assigned to: @leonMSFT on GitHub. # Environment ```none Windows build number: 10.0.18362.175 Windows Terminal version: 0.2.1831.0 PowerShell Core 6.2.1 PowerShell 5.1 ``` I'm on a Microsoft Surface Book. There is no Pause key and no Break key. Ctrl+Break is not an option for stopping a program unless I plug in an external keyboard or do some sort of key remapping specifically for this. # Steps to reproduce Create a script in PowerShell that intercepts Ctrl+C so it can gracefully shut down. Run that script and try to use Ctrl+C. The script can't intercept it. It behaves the same in PowerShell Core and in PowerShell. ```powershell [Console]::TreatControlCAsInput = $True Start-Sleep -Seconds 1 $Host.UI.RawUI.FlushInputBuffer() $exiting = $false while ((-Not $exiting)) { Write-Host "Sleeping..." Start-Sleep -Seconds 1 while ($Host.UI.RawUI.KeyAvailable -and ($Key = $Host.UI.RawUI.ReadKey("AllowCtrlC,NoEcho,IncludeKeyUp"))) { If ([Int]$Key.Character -eq 3) { Write-Warning "Got CTRL-C!" $exiting = $true } $Host.UI.RawUI.FlushInputBuffer() } } Write-Host "Done." [Console]::TreatControlCAsInput = $False ``` # Expected behavior I should be able to hit Ctrl+C to stop the program. In a standard PowerShell terminal (not hosted in the new Microsoft Terminal) this works fine. # Actual behavior The program pauses, ostensibly to allow copy/paste behavior to work, and if I hit Enter it continues running. There is no way to capture Ctrl+C.
Author
Owner

@DHowett-MSFT commented on GitHub (Jul 9, 2019):

This reproduces with pwincon as well. It's not a copy/paste issue, but definitely something in ConPTY. Tagged.

@DHowett-MSFT commented on GitHub (Jul 9, 2019): This reproduces with pwincon as well. It's not a copy/paste issue, but definitely something in ConPTY. Tagged.
Author
Owner

@jcoutch commented on GitHub (Oct 18, 2019):

Just ran into this when using Get-Content -Tail 10 -Wait to tail a log file. Kinda annoying that I can't hit Ctrl+C to return back to the prompt to switch to a different log file...and I'm in the same boat as @tillig with no pause/break key (on a MacBook Pro.)

@jcoutch commented on GitHub (Oct 18, 2019): Just ran into this when using `Get-Content -Tail 10 -Wait` to tail a log file. Kinda annoying that I can't hit Ctrl+C to return back to the prompt to switch to a different log file...and I'm in the same boat as @tillig with no pause/break key (on a MacBook Pro.)
Author
Owner

@analogrelay commented on GitHub (Dec 5, 2019):

EDIT: My issue was unrelated to the main thread. Apologies for any confusion!

I'm getting something similar. Try to cancel a long-running PowerShell command using Ctrl-C just terminates the entire terminal tab instead of cancelling the command. It's very disruptive :(. If this is sounds like a different issue, I can file a separate bug. This does not repro when using pwsh.exe directly.

PowerShellCtrlCBug

(The window disappears when I press Ctrl-C).

@analogrelay commented on GitHub (Dec 5, 2019): **EDIT**: My issue was unrelated to the main thread. Apologies for any confusion! I'm getting something similar. Try to cancel a long-running PowerShell command using Ctrl-C just terminates the entire terminal tab instead of cancelling the command. It's very disruptive :(. If this is sounds like a different issue, I can file a separate bug. This does not repro when using `pwsh.exe` directly. ![PowerShellCtrlCBug](https://user-images.githubusercontent.com/7574/70260701-b1b78c80-1745-11ea-8e18-4982f7e1f6be.gif) (The window disappears when I press Ctrl-C).
Author
Owner

@ssg commented on GitHub (Jan 16, 2020):

This is a breaking issue for me.

@ssg commented on GitHub (Jan 16, 2020): This is a breaking issue for me.
Author
Owner

@analogrelay commented on GitHub (Jan 16, 2020):

FYI, my issue ended up being something unrelated (and a problem on my side, I was launching a wrapper executable around pwsh because I was using scoop and that was terminating from the Ctrl-C). Apologies for any confusion! I don't think it's related to the original issue.

@analogrelay commented on GitHub (Jan 16, 2020): FYI, my issue ended up being something unrelated (and a problem on my side, I was launching a wrapper executable around pwsh because I was using [scoop](https://scoop.sh/) and that was terminating from the Ctrl-C). Apologies for any confusion! I don't think it's related to the original issue.
Author
Owner

@brandedoutcast commented on GitHub (Mar 5, 2020):

Dropping this here to let you know this might've been fixed with PowerShell v7 which comes with a fix to unexpected Ctrl+C behavior powershell/powershell#11959

@brandedoutcast commented on GitHub (Mar 5, 2020): Dropping this here to let you know this might've been fixed with PowerShell `v7` which comes with a fix to unexpected `Ctrl+C` behavior powershell/powershell#11959
Author
Owner

@mithrandir commented on GitHub (Mar 15, 2020):

Installed Powershell Core v7 but still hitting the issue. Cannot get Ctrl-C as a input.

@mithrandir commented on GitHub (Mar 15, 2020): Installed Powershell Core v7 but still hitting the issue. Cannot get Ctrl-C as a input.
Author
Owner

@ghost commented on GitHub (Apr 29, 2020):

:tada:This issue was addressed in #5431, which has now been successfully released as Windows Terminal Preview v0.11.1191.0.🎉

Handy links:

@ghost commented on GitHub (Apr 29, 2020): :tada:This issue was addressed in #5431, which has now been successfully released as `Windows Terminal Preview v0.11.1191.0`.:tada: Handy links: * [Release Notes](https://github.com/microsoft/terminal/releases/tag/v0.11.1191.0) * [Store Download](https://www.microsoft.com/store/apps/9n0dx20hk701?cid=storebadge&ocid=badge)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#2661