-
-
Save pa-0/4594e09bb0eae962b9f2292896ed2c25 to your computer and use it in GitHub Desktop.
Test if PowerShell is running in WindowsTerminal
This file contains hidden or 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
#requires -version 5.1 | |
Function Test-IsWindowsTerminal { | |
[cmdletbinding()] | |
[Outputtype([Boolean])] | |
Param() | |
Write-Verbose "Testing processid $pid" | |
if ($PSVersionTable.PSVersion.major -ge 6) { | |
#PowerShell Core has a Parent property for process objects | |
Write-Verbose "Using Get-Process" | |
$parent = (Get-Process -id $pid).Parent | |
Write-Verbose "Parent process ID is $($parent.id) ($($parent.processname))" | |
if ($parent.ProcessName -eq "WindowsTerminal") { | |
$True | |
} | |
Else { | |
#check the grandparent process | |
$grandparent = (Get-Process -id $parent.id).parent | |
Write-Verbose "Grandarent process ID is $($grandparent.id) ($($grandparent.processname))" | |
if ($grandparent.processname -eq "WindowsTerminal") { | |
$True | |
} | |
else { | |
$False | |
} | |
} | |
} #if Core or later | |
else { | |
#PowerShell 5.1 needs to use Get-CimInstance | |
Write-Verbose "Using Get-CimInstance" | |
$current = Get-CimInstance -ClassName win32_process -filter "processid=$pid" | |
$parent = Get-Process -id $current.parentprocessID | |
Write-Verbose "Parent process ID is $($parent.id) ($($parent.processname))" | |
if ($parent.ProcessName -eq "WindowsTerminal") { | |
$True | |
} | |
Else { | |
#check the grandparent process | |
$cimGrandparent = Get-CimInstance -classname win32_process -filter "Processid=$($parent.id)" | |
$grandparent = Get-Process -id $cimGrandparent.parentProcessId | |
Write-Verbose "Grandarent process ID is $($grandparent.id) ($($grandparent.processname))" | |
if ($grandparent.processname -eq "WindowsTerminal") { | |
$True | |
} | |
else { | |
$False | |
} | |
} | |
} #PowerShell 5.1 | |
} #close function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment