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?
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.
Most helpful comment
@kasini3000 @marvhen I think it's considerably easier to check for the existence of the
WT_SESSIONenvironment variable.