Created
May 15, 2024 22:17
-
-
Save mikefrobbins/2f3085a18f78330e37a5188444e79bc8 to your computer and use it in GitHub Desktop.
Revisions
-
mikefrobbins created this gist
May 15, 2024 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ function Test-IsWindowsTerminal { [CmdletBinding()] param () # Check if PowerShell version is 5.1 or below, or if running on Windows if ($PSVersionTable.PSVersion.Major -le 5 -or $IsWindows -eq $true) { $currentPid = $PID # Loop through parent processes to check if Windows Terminal is in the hierarchy while ($currentPid) { try { $process = Get-CimInstance Win32_Process -Filter "ProcessId = $currentPid" -ErrorAction Stop -Verbose:$false } catch { # Return false if unable to get process information return $false } Write-Verbose -Message "ProcessName: $($process.Name), Id: $($process.ProcessId), ParentId: $($process.ParentProcessId)" # Check if the current process is Windows Terminal if ($process.Name -eq 'WindowsTerminal.exe') { return $true } else { # Move to the parent process $currentPid = $process.ParentProcessId } } # Return false if Windows Terminal is not found in the hierarchy return $false } else { Write-Verbose -Message 'Exiting due to non-Windows environment' return $false } }