Write to event log with PowerShell


Register new source

New-EventLog -LogName 'Application' -Source 'MyApp'

Write to the event log

Write-EventLog -LogName 'Application' -Source 'MyApp' -Message "The PowerShell script has executed successfully" -EventId 1000 -EntryType 'Information'

Fetch events to the console

Get-WinEvent -MaxEvents 10 -FilterHashtable @{logname='security'; id=@(4624)} | select TimeCreated, ProviderName, Id, TaskDisplayName, Message

Fetch events to a file

Get-WinEvent -FilterHashtable @{logname='security'; id=@(4624)} | select TimeCreated, ProviderName, Id, TaskDisplayName, @{n='Message';e={$_.Message -replace '\s+', " "}} | Export-Csv .\eventlog_sec.csv