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

Created on 30 May 2020  ·  4Comments  ·  Source: microsoft/terminal

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?

Issue-Docs Needs-Tag-Fix Needs-Triage

Most helpful comment

@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
}

All 4 comments

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)

Great,3q alot!

@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
}

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrmlnc picture mrmlnc  ·  3Comments

dev-logan picture dev-logan  ·  3Comments

zadjii-msft picture zadjii-msft  ·  3Comments

NickITGuy picture NickITGuy  ·  3Comments

mrmlnc picture mrmlnc  ·  3Comments