Describe how to identity whether a PowerShell is running in Windows Terminal #7627

Closed
opened 2026-01-31 01:08:55 +00:00 by claunia · 4 comments
Owner

Originally created by @hahndorf on GitHub (Apr 24, 2020).

On the Windows Terminal User Documentation page /user-docs/index.md in the Tips and Tricks section, it says one should check for the environment variable WT_Session

When running a PowerShell in the terminal does indeed have this variable, but now run:

Start Powershell.exe

A new PowerShell Window is opened and it is not running in the new terminal, but it inherited the environment variables from its parent, so it also has WT_Session

So this method often works, but not always.

It would be nice to find out inside PowerShell whether it runs in the terminal?

This applies both to PowerShell 5.1 and 7

Originally created by @hahndorf on GitHub (Apr 24, 2020). <!-- Briefly describe which document needs to be corrected and why. --> On the Windows Terminal User Documentation page `/user-docs/index.md` in the Tips and Tricks section, it says one should check for the environment variable `WT_Session` When running a PowerShell in the terminal does indeed have this variable, but now run: Start Powershell.exe A new PowerShell Window is opened and it is not running in the new terminal, but it inherited the environment variables from its parent, so it also has `WT_Session` So this method often works, but not always. It would be nice to find out inside PowerShell whether it runs in the terminal? This applies both to PowerShell 5.1 and 7
claunia added the Issue-QuestionNeeds-Tag-FixResolution-Answered labels 2026-01-31 01:08:55 +00:00
Author
Owner

@DHowett-MSFT commented on GitHub (Apr 24, 2020):

There is, by design, no reliable way. We have been hoping for someone to come along and explain what their use case would be for being able to determine that 😄

@DHowett-MSFT commented on GitHub (Apr 24, 2020): There is, by design, no reliable way. We have been hoping for someone to come along and explain what their use case would be for being able to determine that 😄
Author
Owner

@hahndorf commented on GitHub (Apr 24, 2020):

I can tell how I came across it. I do a lot of stuff in profiles, sometimes I want to reload a session (profile changed) or start a new session as a different user or elevated. I can figure out whether I am in PowerShell.exe or pwsh.exe and start the correct one. It would be nice to open a new session in the same "parent host" or even in a new tab of an existing terminal.

For now the tip is just not 100% correct.

@hahndorf commented on GitHub (Apr 24, 2020): I can tell how I came across it. I do a lot of stuff in profiles, sometimes I want to reload a session (profile changed) or start a new session as a different user or elevated. I can figure out whether I am in `PowerShell.exe` or `pwsh.exe` and start the correct one. It would be nice to open a new session in the same "parent host" or even in a new tab of an existing terminal. For now the tip is just not 100% correct.
Author
Owner

@DHowett-MSFT commented on GitHub (Apr 25, 2020):

So, anything we can do with environment variables is going to be subject to inheritance. The absolute most reliable thing to do will be to detect whether your shell's parent process is WindowsTerminal.exe -- we're not likely to change that name (but I make no promises).

In general, we're trying to go as long as possible not letting it be too easily detectable. We're supposed to be largely xterm-256color compatible, and given our shipping speed we absolutely don't want people working around perceived issues by heuristic detection because it can harm our ability to actually fix things.

In the future with #492 you won't need to make this decision (and all things should start in your preferred default terminal :P)

Marking Question/Answered but please feel free to continue the discussion.

@DHowett-MSFT commented on GitHub (Apr 25, 2020): So, anything we can do with environment variables is going to be subject to inheritance. The absolute most reliable thing to do will be to detect whether your shell's parent process is WindowsTerminal.exe -- we're not likely to change that name (but I make no promises). In general, we're trying to go as long as possible _not_ letting it be too easily detectable. We're supposed to be largely xterm-256color compatible, and given our shipping speed we absolutely don't want people working around perceived issues by heuristic detection because it can harm our ability to actually fix things. In the future with #492 you won't need to make this decision (and all things should start in your preferred default terminal :P) Marking Question/Answered but please feel free to continue the discussion.
Author
Owner

@mileso commented on GitHub (Jun 21, 2021):

There is, by design, no reliable way. We have been hoping for someone to come along and explain what their use case would be for being able to determine that 😄

I have a use case:

Set a different window title when running PowerShell within WIndows Terminal. Windows Terminal has less real estate because the title is displayed on a tab so I'd like to set a shorter title only when running under Windows Terminal.

Simple example: Add the path to the title only when not running within Windows Terminal.

@mileso commented on GitHub (Jun 21, 2021): > There is, by design, no reliable way. We have been hoping for someone to come along and explain what their use case would be for being able to determine that 😄 I have a use case: Set a different window title when running PowerShell within WIndows Terminal. Windows Terminal has less real estate because the title is displayed on a tab so I'd like to set a shorter title only when running under Windows Terminal. Simple example: Add the path to the title only when not running within Windows Terminal.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#7627