GUI windows created by PowerShell appear in the background the first time they're launched after a reboot #17937

Open
opened 2026-01-31 05:59:01 +00:00 by claunia · 5 comments
Owner

Originally created by @corbob on GitHub (Jul 15, 2022).

Windows Terminal version

1.15.1862

Windows build number

10.0.19044.0

Other Software

PowerShell 7.2.5
Windows PowerShell 5.1

Steps to reproduce

  1. In either PowerShell or Windows PowerShell run Get-Process | Out-GridView
  2. Observe that the window appears behind the Terminal
  3. Run the command again
  4. Observe that the window is now appearing in front of the Terminal

note: I have tried to launch notepad.exe one time, and it appeared in front, and after that all other windows also appeared in front. This behaviour also only occurs after a reboot, so to duplicate it again you need to reboot.

Expected Behavior

No response

Actual Behavior

2022-07-15_13-24-43 (1)

Originally created by @corbob on GitHub (Jul 15, 2022). ### Windows Terminal version 1.15.1862 ### Windows build number 10.0.19044.0 ### Other Software PowerShell 7.2.5 Windows PowerShell 5.1 ### Steps to reproduce 1. In either PowerShell or Windows PowerShell run `Get-Process | Out-GridView` 2. Observe that the window appears behind the Terminal 3. Run the command again 4. Observe that the window is now appearing in front of the Terminal **note:** I have tried to launch `notepad.exe` one time, and it appeared in front, and after that all other windows also appeared in front. This behaviour also only occurs after a reboot, so to duplicate it again you need to reboot. ### Expected Behavior _No response_ ### Actual Behavior ![2022-07-15_13-24-43 (1)](https://user-images.githubusercontent.com/30301021/179306567-4cf944c2-2ff2-4d8b-85e4-65f1ba0a8323.gif)
claunia added the Issue-BugProduct-TerminalPriority-2Area-WindowingArea-DefApp labels 2026-01-31 05:59:02 +00:00
Author
Owner

@zadjii-msft commented on GitHub (Jul 18, 2022):

This behaviour also only occurs after a reboot, so to duplicate it again you need to reboot.

wat

That's crazy if that's true. Did you have the terminal set up to automatically launch on startup/? That might explain why we wouldn't have gotten a focus event. Theoretically, this should have been fixed by the pile of PRs linked to #2988, but they would require the terminal to get focused while it was the foreground window.

@zadjii-msft commented on GitHub (Jul 18, 2022): > This behaviour also only occurs after a reboot, so to duplicate it again you need to reboot. _wat_ That's crazy if that's true. Did you have the terminal set up to automatically launch on startup/? That might explain why we wouldn't have gotten a focus event. Theoretically, this should have been fixed by the pile of PRs linked to #2988, but they would require the terminal to get focused while it was the foreground window.
Author
Owner

@ghost commented on GitHub (Jul 22, 2022):

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@ghost commented on GitHub (Jul 22, 2022): This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for **4 days**. It will be closed if no further activity occurs **within 3 days of this comment**.
Author
Owner

@corbob commented on GitHub (Jul 24, 2022):

I don't have an update I can provide at the moment as I'm not near a computer, and I may not get a chance for a few days, but I don't want this to close just yet, so I'll leave a comment with my intentions to follow up by Tuesday or Wednesday.

Turns out I was able to get on the computer to type more gooder:

This behaviour also only occurs after a reboot, so to duplicate it again you need to reboot.

wat

That's crazy if that's true. Did you have the terminal set up to automatically launch on startup/? That might explain why we wouldn't have gotten a focus event. Theoretically, this should have been fixed by the pile of PRs linked to #2988, but they would require the terminal to get focused while it was the foreground window.

So to clarify this, I do not have terminal set to auto-launch. What I mean is that this behaviour happens the first time I load pwsh.exe, or powershell.exe. Once that's done, I cannot reproduce until either logging off or restarting the computer.

@corbob commented on GitHub (Jul 24, 2022): ~~I don't have an update I can provide at the moment as I'm not near a computer, and I may not get a chance for a few days, but I don't want this to close just yet, so I'll leave a comment with my intentions to follow up by Tuesday or Wednesday.~~ Turns out I was able to get on the computer to type more gooder: > > This behaviour also only occurs after a reboot, so to duplicate it again you need to reboot. > > _wat_ > > That's crazy if that's true. Did you have the terminal set up to automatically launch on startup/? That might explain why we wouldn't have gotten a focus event. Theoretically, this should have been fixed by the pile of PRs linked to #2988, but they would require the terminal to get focused while it was the foreground window. So to clarify this, I do not have terminal set to auto-launch. What I mean is that this behaviour happens the first time I load `pwsh.exe`, or `powershell.exe`. Once that's done, I cannot reproduce until either logging off or restarting the computer.
Author
Owner

@ajansveld commented on GitHub (Sep 26, 2022):

I can confirm this behavior still exists in Terminal Preview 1.16.2523.0

@ajansveld commented on GitHub (Sep 26, 2022): I can confirm this behavior still exists in Terminal Preview 1.16.2523.0
Author
Owner

@micolous commented on GitHub (Oct 5, 2022):

This seems like a repeat of #6633, which suggests #2988 isn't actually fixed.

I see the similar behaviour on new Powershell tabs in Terminal 1.15.2712.0: the first time I run Get-Process | Out-GridView, the dialog appears behind Terminal, but the second and subsequent times, it appears in front.

The test in #6633 uses System.Windows.Forms.FolderBrowserDialog - this now always shows the browser dialog in front of Terminal (regardless of whether you set TopMost = true), but the dialog never gets focus.

The easiest reliable way I've found for a console application running in Terminal to get a window to appear on-top and with focus is to use the random SetConsoleTitle/FindWindow hack. Using GetConsoleWindow is unreliable, and it's not clear from reading Windows API docs what you're supposed to do instead.

https://github.com/mgbowen/windows-fido-bridge/blob/master/src/win32_middleware_common/src/window.cpp has another implementation, which creates a new window and tries to give it focus (SetForegroundWindow), such that any other windows parented on it (such as the GUI-only Windows WebAuthn APIs) get proper focus and z-order. Even then it needs special hacks to work with VS Code (which currently blames Terminal for z-order bugs).

@micolous commented on GitHub (Oct 5, 2022): This seems like a repeat of #6633, which suggests #2988 isn't actually fixed. I see the similar behaviour on _new_ Powershell tabs in Terminal 1.15.2712.0: the first time I run `Get-Process | Out-GridView`, the dialog appears behind Terminal, but the second and subsequent times, it appears in front. The test in #6633 uses `System.Windows.Forms.FolderBrowserDialog` - this now _always_ shows the browser dialog in front of Terminal (regardless of whether you set `TopMost = true`), but the dialog _never_ gets focus. The easiest reliable way I've found for a console application running in Terminal to get a window to appear on-top _and_ with focus is to use the [random `SetConsoleTitle`/`FindWindow` hack](https://learn.microsoft.com/en-us/troubleshoot/windows-server/performance/obtain-console-window-handle). Using `GetConsoleWindow` is unreliable, and it's not clear from reading Windows API docs what you're supposed to do instead. https://github.com/mgbowen/windows-fido-bridge/blob/master/src/win32_middleware_common/src/window.cpp has another implementation, which creates a new window and tries to give it focus (`SetForegroundWindow`), such that any other windows parented on it (such as the GUI-only Windows WebAuthn APIs) get proper focus and z-order. Even then it needs special hacks to work with VS Code (which [currently blames Terminal for z-order bugs](https://github.com/microsoft/vscode/issues/42356)).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#17937