Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I determine if my PowerShell script is running in Windows terminal? #6269

Closed
kasini3000 opened this issue May 30, 2020 · 10 comments
Closed
Labels
Issue-Docs It's a documentation issue that really should be on MicrosoftDocs/Console-Docs Needs-Tag-Fix Doesn't match tag requirements Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting

Comments

@kasini3000
Copy link

How can I determine if my PowerShell script is running in Windows terminal?

I wrote a script, a.ps1, which contains some special unicode characters.
I want to check whether my script is running in the powershell executed by windows terminal. How can I detect the windows terminal host?

@kasini3000 kasini3000 added the Issue-Docs It's a documentation issue that really should be on MicrosoftDocs/Console-Docs label May 30, 2020
@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels May 30, 2020
@marvhen
Copy link

marvhen commented May 30, 2020

This is working for me:

#Is-WindowsTerminal.ps1

function IsWindowsTerminal ($childProcess) {
    if (!$childProcess) {
        return $false
    } elseif ($childProcess.ProcessName -eq 'WindowsTerminal') {
        return $true
    } else {
        return IsWindowsTerminal -childProcess $childProcess.Parent
    }
}

return IsWindowsTerminal -childProcess (Get-Process -Id $PID)

@kasini3000
Copy link
Author

Great,3q alot!

@oising
Copy link
Collaborator

oising commented May 31, 2020

@kasini3000 @marvhen I think it's considerably easier to check for the existence of the WT_SESSION environment variable.

function Test-WindowsTerminal { test-path env:WT_SESSION }

# or

if ($env:WT_SESSION) { 
    # yes, windows terminal
} else {
    # nope
}

@marvhen
Copy link

marvhen commented May 31, 2020

Indeed...much easier. I was not aware of that variable, but now see it is documented here.

@TitoAldarondo
Copy link

TitoAldarondo commented Feb 16, 2021

So I too initially headed down the route of checking $Env:WT_SESSION, but quickly realized that this wouldn't work when SSHing to other hosts. Instead, I decided to simply check for Unicode support in the current session:

function Test-IsEmojiFriendly {
  if ("$OutputEncoding".EndsWith('UTF8Encoding')) {
    return $true
  }
  return $false
}

There are probably other terminal-specific considerations here, but hopefully this helps.

@DHowett
Copy link
Member

DHowett commented Feb 16, 2021

That's just going to tell you if PowerShell thinks it can send UTF-8 data somewhere; it will not tell you anything about the capabilities of the terminal it's talking to.

@gerardog
Copy link

FYI. I just rewrote IsWindowsTerminal but with support for WT as default terminal app, in this comment.

shaneharper added a commit to shaneharper/dotfiles that referenced this issue Oct 26, 2023
This commit deletes the is_Windows_Terminal funcref added in 6390072; The lambda's definition was incorrect, e.g. after starting "Command Prompt" from the Windows 11 Start Menu the lambda would return false even when Windows Terminal was being used. (See microsoft/terminal#13006. microsoft/terminal#6269 (comment) claims to have code to check whether a program is running in Windows Terminal or not.)

There may be some Windows terminal programs that only support 16 colors, but that's not relevant to me.
@mrkey7

This comment was marked as off-topic.

@gerardog
Copy link

function IsWTrun {'WindowsTerminal' -in (Get-Process).Name}

That returns true if any instance of WindowsTerminal is running. PowerShell could be running on an old Console Host and still return true if a WT is open.

@mrkey7
Copy link

mrkey7 commented May 4, 2024

function IsWTrun {'WindowsTerminal' -in (Get-Process).Name}

That returns true if any instance of WindowsTerminal is running. PowerShell could be running on an old Console Host and still return true if a WT is open.

Thanks. Do you have a 100% valid method? Looking for $Env:WT_SESSION not valid too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Docs It's a documentation issue that really should be on MicrosoftDocs/Console-Docs Needs-Tag-Fix Doesn't match tag requirements Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting
Projects
None yet
Development

No branches or pull requests

7 participants