Skip to content

Instantly share code, notes, and snippets.

@mikefrobbins
Created May 15, 2024 22:17
Show Gist options
  • Save mikefrobbins/2f3085a18f78330e37a5188444e79bc8 to your computer and use it in GitHub Desktop.
Save mikefrobbins/2f3085a18f78330e37a5188444e79bc8 to your computer and use it in GitHub Desktop.

Revisions

  1. mikefrobbins created this gist May 15, 2024.
    35 changes: 35 additions & 0 deletions Test-IsWindowsTerminal.ps1
    Original 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
    }
    }