It looks like the Get-WinEvent cmdlet writes the error message even if the ErrorAction is set to SilentlyContinue. To avoid the message being shown on the console, wrap the cmdlet and the code that follows in a Try/Catch block. You can deal with the error in the catch block if you like.
$startdate = "01/01/2022 01:00:00" # mm/tt/yyyy
$enddate = "12/31/2022 23:59:00" # Was "12/31/2022 23:590:00"
#$Computer = "PCABD"
#===========================================================================================================
# Get all Eventlogs
#===========================================================================================================
$AllEventLogs = Get-WinEvent -ListLog Microsoft-Windows* #-ComputerName PCAF1
foreach ($EventLog in $AllEventLogs) {
try{
#Get-WinEvent -ListLog *
$Events = Get-WinEvent -LogName $EventLog.LogName -ErrorAction STOP |
Where-Object { $_.TimeCreated -gt $startdate -and $_.TimeCreated -lt $enddate }
foreach ($event in $Events) {
If($event.Message -match "Suchbegriff"){
Write-Host $event.Message -ForegroundColor Green
$event.LogName + ";" + $event.TimeCreated + ";" + $event.id + ";" + $event.LevelDisplayName + ";" + $event.Message >>c:\junk\EventLogTreffer.txt
} # endif
}
}
catch{
# ignore the error
}
} #foreach AllEventlo