Skip to content
Caleb SmithCyberSecurityWithSmitti
Browse PowerShell example contentPowerShell example

05 · Get-WinEvent: filter failed sign-ins

Use a server-side event filter and cap the number of returned records.

How to use it

Query the last 24 hours of local failed-logon events. Empty results are reported clearly; other collection failures remain visible.

Platform and permissions

Windows; Security log access and relevant audit policy are needed.

What changes / what is read

Reads the event log. No log is cleared.

Scope and interpretation

NoMatchingEventsFound, access denied and an unavailable channel mean different things. Missing results do not establish that no failure occurred. Event messages can contain sensitive identifiers.

Practice task

Explain the difference between a log query error and a verified empty result.

Reference

Microsoft documentation. Review the documentation for your installed version and use only an authorised learning or investigation environment.

This is personal educational material. Review the complete code and your signing policy before use. PowerShell code is displayed and downloaded as text; WordPress does not run it. Collection caps limit rows/files, not every provider’s wall-clock execution time.

PowerShell · PowerShell 5.1+ syntax target; Windows collectors require the stated Windows modules. Full Windows acceptance testing remains required.Download .ps1
#requires -Version 5.1
try {
    Get-WinEvent -FilterHashtable @{
        LogName = 'Security'
        Id = 4625
        StartTime = (Get-Date).AddHours(-24)
    } -MaxEvents 50 -ErrorAction Stop |
        Select-Object TimeCreated, Id, RecordId, Message
}
catch {
    if ($_.FullyQualifiedErrorId -like 'NoMatchingEventsFound*') {
        Write-Host 'No matching failed-logon events found in the last 24 hours.'
    }
    else {
        Write-Warning ("Security log query failed: {0}" -f $_.Exception.Message)
    }
}

Example code is displayed as text. Review it and test in your own lab before use.