Exiting a PowerShell tab does not trigger the Powershell.Exiting event. #4485

Closed
opened 2026-01-30 23:48:54 +00:00 by claunia · 1 comment
Owner

Originally created by @danielniccoli on GitHub (Oct 15, 2019).

Environment

Windows build number: 10.0.18999.0
Windows Terminal version: 0.5.2762.0

Steps to reproduce

  1. Open a new PowerShell tab. PowerShell version does not matter.
  2. Paste and run the following code in the PowerShell tab:
Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action { Set-Content -Value "Hello there." -Path "$env:TEMP\General Kenobi.txt" }
  1. Close the PowerShell tab by clicking the x button or by the exit command. Or close the Terminal window.

grafik

Expected behavior

The PowerShell tab should trigger the Powershell.Exiting event and a file %temp%\General Kenobi.txt should be created.

Actual behavior

The event does not trigger and no file is created.

Note: If you reproduce this in the native PowerShell 5 or 7-preview.3 console (not the Terminal app) you'll find that the event triggers correctly only when using the exit command. Clicking the close button also does not trigger the event. Although I also reported that at https://github.com/PowerShell/PowerShell/issues/8000, I believe that with Terminal it's an independent issue.

Originally created by @danielniccoli on GitHub (Oct 15, 2019). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> <!-- This bug tracker is monitored by Windows Terminal development team and other technical folks. **Important: When reporting BSODs or security issues, DO NOT attach memory dumps, logs, or traces to Github issues**. Instead, send dumps/traces to secure@microsoft.com, referencing this GitHub issue. If this is an application crash, please also provide a Feedback Hub submission link so we can find your diagnostic data on the backend. Use the category "Apps > Windows Terminal (Preview)" and choose "Share My Feedback" after submission to get the link. Please use this form and describe your issue, concisely but precisely, with as much detail as possible. --> # Environment ```none Windows build number: 10.0.18999.0 Windows Terminal version: 0.5.2762.0 ``` # Steps to reproduce 1. Open a new PowerShell tab. PowerShell version does not matter. 2. Paste and run the following code in the PowerShell tab: ```powershell Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action { Set-Content -Value "Hello there." -Path "$env:TEMP\General Kenobi.txt" } ``` 4. Close the PowerShell tab by clicking the x button or by the `exit` command. Or close the Terminal window. ![grafik](https://user-images.githubusercontent.com/2971735/66866265-3a614a00-ef99-11e9-9dd1-d33e4fd371cb.png) # Expected behavior The PowerShell tab should trigger the Powershell.Exiting event and a file `%temp%\General Kenobi.txt` should be created. # Actual behavior The event does not trigger and no file is created. Note: If you reproduce this in the native PowerShell 5 or 7-preview.3 console (not the Terminal app) you'll find that the event triggers correctly only when using the `exit` command. Clicking the close button also does not trigger the event. Although I also reported that at https://github.com/PowerShell/PowerShell/issues/8000, I believe that with Terminal it's an independent issue.
claunia added the Needs-TriageResolution-ExternalNeeds-Tag-FixProduct-Powershell labels 2026-01-30 23:48:54 +00:00
Author
Owner

@DHowett-MSFT commented on GitHub (Oct 15, 2019):

I just tried to reproduce this, and it does work in Terminal when you exit. It doesn't work on tab/window close, but that is almost certainly for the same reason as PowerShell/PowerShell#8000.

Powershell can determine when its console is being closed -- it should receive a CTRL_CLOSE_EVENT through its CtrlEvent handler. This is true regardless of whether terminal or conhost is in active use.

It looks like this is just an External issue.

@DHowett-MSFT commented on GitHub (Oct 15, 2019): I just tried to reproduce this, and it _does_ work in Terminal when you `exit`. It doesn't work on tab/window close, but that is almost certainly for the same reason as PowerShell/PowerShell#8000. Powershell can determine when its console is being closed -- it should receive a `CTRL_CLOSE_EVENT` through its `CtrlEvent` handler. This is true regardless of whether terminal or conhost is in active use. It looks like this is just an External issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#4485