Skip to content

Get-ProcessInfo

Windows: Retrieves detailed information about running processes

#Requires -Version 5.1

[CmdletBinding()]
Param
(
    [string]$ComputerName = $env:COMPUTERNAME,

    [string]$Name,

    [int]$Id,

    [switch]$IncludeUserName,

    [pscredential]$Credential
)

Process {
    try {
        $processParams = @{
            'ErrorAction' = 'Stop'
        }

        if (-not [string]::IsNullOrWhiteSpace($Name)) {
            $processParams.Add('Name', $Name)
        }
        elseif ($Id -gt 0) {
            $processParams.Add('Id', $Id)
        }

        if ($IncludeUserName) {
            $processParams.Add('IncludeUserName', $true)
        }

        if ($ComputerName -ne $env:COMPUTERNAME) {
            $invokeParams = @{
                'ComputerName' = $ComputerName
                'ScriptBlock'  = {
                    Param($Params)
                    Get-Process @Params | Select-Object Name, Id, CPU, WorkingSet, StartTime, Path, Company, UserName
                }
                'ArgumentList' = $processParams
                'ErrorAction'  = 'Stop'
            }
            if ($null -ne $Credential) {
                $invokeParams.Add('Credential', $Credential)
            }

            $result = Invoke-Command @invokeParams
        }
        else {
            $result = Get-Process @processParams | Select-Object Name, Id, CPU, WorkingSet, StartTime, Path, Company, UserName
        }

        $results = foreach ($proc in $result) {
            [PSCustomObject]@{
                Name         = $proc.Name
                Id           = $proc.Id
                CPU          = if ($proc.CPU) { [math]::Round($proc.CPU, 2) } else { 0 }
                MemoryMB     = if ($proc.WorkingSet) { [math]::Round($proc.WorkingSet / 1MB, 2) } else { 0 }
                StartTime    = $proc.StartTime
                User         = $proc.UserName
                Path         = $proc.Path
                Company      = $proc.Company
                ComputerName = $ComputerName
            }
        }

        Write-Output ($results | Sort-Object CPU -Descending)
    }
    catch {
        throw
    }
}

Specifies the name of the computer to query. Defaults to the local computer.

Specifies the name of the process to retrieve. Supports wildcards.

Specifies the process ID (PID) to retrieve specifically.

Off

If set, attempts to retrieve the user account under which each process is running (Requires elevation).

Specifies a PSCredential object for remote connection.

An interactive directory of PowerShell scripts.