SolvedPowerShell pwsh as a dotnet global tool exhibits very unusual behavior with Ctrl-C

Steps to reproduce

Install powershell as a dotnet global tool. Run a long-running thing (like Start-Sleep), and then press Ctrl+C

Expected behavior

I am returned to a powershell prompt.

C:\Users\duhowett>.\.dotnet\tools\pwsh
PowerShell 6.2.3

(dhowett-dev2) ~ % start-sleep 10
^C
(dhowett-dev2) ~ % # i am returned to a powershell prompt

Actual behavior

The dotnet tool shim hosting powershell terminates and returns control to cmd, but powershell remains connected to the console and reads input simultaneously. PowerShell and CMD share the input buffer, and neither of them can agree on what goes into it.
If you manage to exit cmd, powershell re-asserts control.

The dir in the snippet below came from cmd

C:\Users\duhowett>.\.dotnet\tools\pwsh
PowerShell 6.2.3

(dhowett-dev2) ~ % start-sleep 10
^C
C:\Users\duhowett>
(dhowett-dev2) ~ % dir
 Volume in drive C is Cesium
 Volume Serial Number is E499-450A

 Directory of C:\Users\duhowett

image

The blue d came from powershell, and the ir came from cmd.

Environment data

Name                           Value
----                           -----
PSVersion                      6.2.3
PSEdition                      Core
GitCommitId                    6.2.3
OS                             Microsoft Windows 10.0.19041
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
26 Answers

✔️Accepted Answer

Unless Scoop is using the dotnet global tool wrapper shipped with PowerShell, this is likely an issue affecting all Scoop-shimmed console-subsystem binaries. It’s almost certainly worth filing a duplicate of this issue on them so they can fix it for everyone.

Pretty simple fix in the end, just wasn't immediately obvious to me so here is what I did.

Just edit Windows Terminal settings such that your using pwsh.exe directly and not using the scoop shim.

Originally I had:

{
    "guid": "{b91d6aa8-b68c-416b-89b4-f29a2abebdcd}",
    "name": "Windows PowerShell Core",
    "commandline": "pwsh.exe", // which resolves to C:\Users\brad.jones\scoop\shims\pwsh.exe
    "hidden": false,
    "fontSize": 10,
    "padding": "1",
    "icon": "C:\\Users\\brad.jones\\Pictures\\PowerShell_Core_6.0_icon.png",
    "startingDirectory": "%USERPROFILE%"
}

Updated to:

{
    "guid": "{b91d6aa8-b68c-416b-89b4-f29a2abebdcd}",
    "name": "Windows PowerShell Core",
    "commandline": "C:\\Users\\brad.jones\\scoop\\apps\\powershell-core\\current\\pwsh.exe",
    "hidden": false,
    "fontSize": 10,
    "padding": "1",
    "icon": "C:\\Users\\brad.jones\\Pictures\\PowerShell_Core_6.0_icon.png",
    "startingDirectory": "%USERPROFILE%"
}

Other Answers:

@SteveL-MSFT This is interesting and we should figure this out. I means that anyone using PowerShell like this loses the whole process when pressing Ctrl-C

@KathleenDollard for visibility. This makes PowerShell installed as a dotnet global tool not really usable.
image

@brad-jones
Awesome. So simple and I can confirm that it works perfect.

            "commandline": "%USERPROFILE%\\scoop\\apps\\pwsh\\current\\pwsh.exe",

🎉This issue was addressed in #11959, which has now been successfully released as v7.0.0.🎉

Handy links:

FWIW, I'm seeing this behavior in Windows Terminal with PowerShell installed via https://scoop.sh. Both 6.2.3 and 7.0.0-rc1 are acting similarly for me. Here's the output from 7.0.0-rc1:

C:\Users\brian>where pwsh
C:\Users\brian\scoop\shims\pwsh.exe

C:\Users\brian>pwsh --version
PowerShell 7.0.0-rc.1

C:\Users\brian>pwsh
PowerShell 7.0.0-rc.1
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/powershell
Type 'help' to get help.

PS C:\Users\brian> start-sleep 10
^C
C:\Users\brian>
PS C:\Users\brian> gci
'gci' is not recognized as an internal or external command,
operable program or batch file.

Starting in cmd.exe, we can see that PowerShell version 7.0.0-rc.1 is the default pwsh and is located at C:\Users\brian\scoop\shims\pwsh.exe.

Running start-sleep 10, then pressing ctrl+c to stop the sleep results in a return to cmd.exe, with the PS prompt being output once for some reason. Attempting to run a PowerShell cmdlet results in a failure showing that we are, in fact, in cmd.exe.

More Issues: