PowerShell 2.0: One Cmdlet at a Time 97 Get-Event

Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Get-Event cmdlet.

What can I do with it?

Retrieve events from the event queue.


The built-in PowerShell help has a great example for New-Event. It uses New-Event to create a custom event based on a reaction to another event. Once the event has been created Get-Event can be used to examine details of that event and any others currently in the queue.

function Enable-ProcessCreationEvent { $query = New-Object System.Management.WqlEventQuery “__InstanceCreationEvent”, (New-Object TimeSpan 0,0,1), “TargetInstance isa ‘Win32_Process’” $processWatcher = New-Object System.Management.ManagementEventWatcher $query $identifier = “WMI.ProcessCreated” Register-ObjectEvent $processWatcher “EventArrived” -SupportEvent $identifier -Action { [void] (New-Event -sourceID “PowerShell.ProcessCreated” -Sender $args[0] -EventArguments $args[1].SourceEventArgs.NewEvent.TargetInstance) } } Get-Event

You will notice that if you execute this function and then create a new process a new event is automatically generated. Get-Event retrieves details of this event.

How could I have done this in PowerShell 1.0?

PowerShell engine events are a new feature in PowerShell 2.0.

1000 things 1% better