'How to detect if running in the new Windows Terminal?

An upcoming feature of the Windows Terminal preview is that it has full emoji support:

Windows Powershell running in Windows Terminal

Compared to:

Windows Powershell

In Node.js, how do I detect if I'm running in a terminal wrapped by the Windows Terminal instead of its "naked" variation? Is there an environmental variable I can extract or a synchronous test I can do?



Solution 1:[1]

You can check for the WT_SESSION environmental variable which is set to a v4 UUID: https://github.com/microsoft/terminal/issues/1040

If you're looking for a quick and dirty way to check, this should work:

!!process.env.WT_SESSION

There's also a more elaborate method you can use, taking advantage of is-uuid, is-wsl and process.platform:

import isUUID from 'is-uuid';
import isWsl from 'is-wsl';

const isWindowsTerminal = (process.platform === "win32" || isWsl) && isUUID.v4(process.env.WT_SESSION);

Solution 2:[2]

I prefer this approach from https://github.com/microsoft/terminal/issues/6269 (in PowerShell):

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

which I then use in my profile to turn on e.g. oh-my-posh.

$IsWindowsTerminal = IsWindowsTerminal -childProcess (Get-Process -Id $PID)
if($IsWindowsTerminal) {
    oh-my-posh --init --shell pwsh --config $HOME\Documents\mytheme.omp.json | Invoke-Expression
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dan Atkinson