<< Back to Script Library

Identify associated Windows services

Finds and displays the details of all Windows services associated with the selected process.
Version: 1.2.2
Created: 2014-09-18
Modified: 2014-09-18
Creator: ControlUp Support
Downloads: 1404
Tags:
The Script Copy Script Copied to clipboard
$computer = "."
$InputProcessId = $args[0]
if ($args[0] -eq $null) {
    Write-Host "This script expects the PID as a single argument."
    Exit 1
} else {
    try {
        $ProcObject = Get-WmiObject -Class Win32_Process -ComputerName $computer -Filter "ProcessId=$InputProcessId" -ErrorAction Stop
    } catch {}
    if ($ProcObject -eq $null) {
        Write-Host "The selected process was not found on this system."
        Exit 1
    } else {
        $results = ($ProcObject | ForEach {
            $process = $_
                Get-WmiObject -Class Win32_Service -ComputerName $computer -Filter "ProcessId=$($_.ProcessId)" | % {
                    New-Object PSObject -Property @{ProcessId=$process.ProcessId;
                                                                            ServiceName=$_.Name;
                                                                            State=$_.State;
                                                                            DisplayName=$_.DisplayName;
                                                                            StartMode=$_.StartMode}
                    }
        })
        if ($results -eq $null) {
            Write-Host "The selected process is not associated with any Windows services."
        } else {
            Write-Host "The selected process is associated with the following Windows service/s:`r`n"
            $results | ft -AutoSize ServiceName,DisplayName,State,StartMode
        }
    }
}