How to determine if an EventLog already exists How to determine if an EventLog already exists powershell powershell

How to determine if an EventLog already exists


# Check if Log exists# Ref: http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.exists(v=vs.110).aspx[System.Diagnostics.EventLog]::Exists('Application');# Ref: http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.sourceexists(v=vs.110).aspx# Check if Source exists[System.Diagnostics.EventLog]::SourceExists("YourLogSource");


So I was on the right path with Get-EventLog.

Instead of just reading it, I stored it in a variable. Then I checked if the variable was null.

This has achieved what I was looking to do.

$logFileExists = Get-EventLog -list | Where-Object {$_.logdisplayname -eq "Visual Studio Builds"} if (! $logFileExists) {    New-EventLog -LogName "Visual Studio Builds" -Source "Visual Studio"}


if ([System.Diagnostics.EventLog]::SourceExists("Visual Studio") -eq $False) {    New-EventLog -LogName "Visual Studio Builds" -Source "Visual Studio"}