7

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?

Richie Bendall
  • 7,738
  • 4
  • 38
  • 58

2 Answers2

8

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);
Richie Bendall
  • 7,738
  • 4
  • 38
  • 58
  • 1
    Note: `WT_SESSION` is not always set on Windows 11 – cuzi Jan 24 '23 at 08:57
  • Caveats: 1. If you run `cmd` inside Windows Terminal, or run a GUI program that launches another terminal emulator, this env var will be inherited. 2. If you run via ssh, WT_SESSION will not be set. 3. If you use Open Powershell Here in Windows Explorer, WT_SESSION will not be set, because `powershell.exe` is launched outside of the Terminal app, then later attached to it. (Source: https://github.com/microsoft/terminal/issues/11057) – 1j01 Jul 23 '23 at 04:48
  • Also, I would advise *against* checking that the value is a GUID. It seems this would only make it annoying to manually force detection of Windows Terminal, and make it less future proof in case they decide to change the format. I don't see any scenario where a change in the format would make it less of an indicator of running in the Windows Terminal, so better just keep it simple. – 1j01 Jul 23 '23 at 04:59
  • `WT_SESSION` does not appear to be set by Visual Studio 2022 when running a console app under the debugger. – tig Aug 17 '23 at 19:00
1

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
}
Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Kai Walter
  • 3,485
  • 2
  • 32
  • 62
  • If I run this under Windows Terminal, it returns `false`. `(Get-Process -Id $PID).ProcessName` is "powershell" and there is no `Parent` property for $childProcess. So it hits the else on the first run and then hits the `!$childProcess` on the second. – Dan Atkinson May 04 '22 at 10:56