Forked from mikefrobbins/Test-IsWindowsTerminal.ps1
Last active
October 11, 2024 01:09
-
-
Save aproposts/c3f752157714d7174d99cae119c29c47 to your computer and use it in GitHub Desktop.
Determine if the host is Windows Terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-IsWindowsTerminal { | |
<# | |
Adapted from | |
https://mikefrobbins.com/2024/05/16/detecting-windows-terminal-with-powershell/ | |
Changed to use the Parent property from Get-Process in pwsh (if possible) to | |
avoid loading the CimCmdlets module. Since I use this test in my $PROFILE, | |
it speeds things up and avoids unneccessary clutter. | |
#> | |
[CmdletBinding()] | |
param () | |
if ($IsWindows) { | |
function isWindowsTerminal([UInt32] $Id) { | |
try { | |
Get-Process -Id $Id -ErrorAction Stop -Verbose:$false | | |
ForEach-Object { | |
'ProcessName: {0}, Id: {1}, ParentId: {2}' -f @( | |
$_.Name, | |
$_.Id, | |
$_.Parent.Id | |
) | Write-Verbose | |
if ($_.Name -eq 'WindowsTerminal') { return $true } | |
elseif ($_.Parent.Id) { return isWindowsTerminal($_.Parent.Id) } | |
else { return $false } | |
} | |
} | |
catch { return $false } | |
} | |
} | |
else { | |
function isWindowsTerminal([UInt32] $Id) { | |
try { | |
Get-CimInstance Win32_Process -Filter "ProcessId = $Id" -ErrorAction Stop -Verbose:$false | | |
ForEach-Object { | |
'ProcessName: {0}, Id: {1}, ParentId: {2}' -f @( | |
$_.Name, | |
$_.ProcessId, | |
$_.ParentProcessId | |
) | Write-Verbose | |
if ($_.Name -eq 'WindowsTerminal.exe') { return $true } | |
elseif ($_.ParentProcessId) { return isWindowsTerminal($_.ParentProcessId) } | |
else { return $false } | |
} | |
} | |
catch { return $false } | |
} | |
} | |
# Check if PowerShell version is 5.1 or below, or if running on Windows | |
if ($IsWindows -or $PSVersionTable.PSVersion.Major -le 5) { | |
return isWindowsTerminal($PID) | |
} | |
else { | |
Write-Verbose -Message 'Exiting due to non-Windows environment' | |
return $false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment