Recipes by topic

Get notified when a process starts

Register-CimIndicationEvent subscribes to a WMI event. For processes, the Win32_ProcessStartTrace event class fires at each start; it requires an elevated PowerShell console. Without elevation, the __InstanceCreationEvent query on Win32_Process (see below) polls at a regular interval.

$registerParameters = @{
    Query            = 'SELECT * FROM Win32_ProcessStartTrace'
    SourceIdentifier = 'ProcessStart'
}
Register-CimIndicationEvent @registerParameters

try {
    $endTime = (Get-Date).AddSeconds(60)
    while ((Get-Date) -lt $endTime) {
        $processEvent = Wait-Event -SourceIdentifier 'ProcessStart' -Timeout 5
        if ($processEvent) {
            $newEvent = $processEvent.SourceEventArgs.NewEvent
            [PSCustomObject]@{
                Time            = $processEvent.TimeGenerated
                ProcessName     = $newEvent.ProcessName
                ProcessId       = $newEvent.ProcessID
                ParentProcessId = $newEvent.ParentProcessID
            }
            Remove-Event -EventIdentifier $processEvent.EventIdentifier
        }
    }
} finally {
    Unregister-Event -SourceIdentifier 'ProcessStart'
}

-SourceIdentifier names the subscription. Wait-Event waits for an event in the session queue (here for 60 seconds, in slices of 5) and Remove-Event removes it from the queue. The finally block calls Unregister-Event: without it, the subscription keeps queueing events until the console is closed.

Variant: without elevation, with -Action

This query polls Win32_Process every 2 seconds (WITHIN 2): a process that lives for less time can go unnoticed.

$query = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_Process'"
$action = {
    $process = $Event.SourceEventArgs.NewEvent.TargetInstance
    Write-Output -InputObject ('{0} ({1})' -f $process.Name, $process.ProcessId)
}
$registerParameters = @{
    Query            = $query
    SourceIdentifier = 'ProcessCreated'
    Action           = $action
}
Register-CimIndicationEvent @registerParameters

# Later: read what the action produced, then clean up
Receive-Job -Name 'ProcessCreated'
Unregister-Event -SourceIdentifier 'ProcessCreated'
Remove-Job -Name 'ProcessCreated' -Force

With -Action, PowerShell creates a background job with the same name as -SourceIdentifier: the output of the action is read with Receive-Job. Unregister-Event removes the subscription, Remove-Job removes the job.

Variant: remote

Add -CimSession to Register-CimIndicationEvent to watch another machine:

$cimSession = New-CimSession -ComputerName 'SERVER01'
$registerParameters = @{
    CimSession       = $cimSession
    Query            = 'SELECT * FROM Win32_ProcessStartTrace'
    SourceIdentifier = 'RemoteProcessStart'
}
Register-CimIndicationEvent @registerParameters

# When monitoring is done
Unregister-Event -SourceIdentifier 'RemoteProcessStart'
Remove-CimSession -CimSession $cimSession

Classes used

← All recipes