Back to script library
Entra / Microsoft 365 · Compliance & audit

Report audit rec failed sign in

Search the unified audit log for failed user login audit records.

Connect & set up

Run these once per session. All scopes are read-only unless the script makes changes.

Connect-ExchangeOnline -SkipLoadingCmdletHelp

Run it

The main script. Copy it, or download the .ps1 and run it from your console.

param(
[int] $LookbackDays = 90
)
$Modules = Get-Module | Select-Object -ExpandProperty Name
If ("ExchangeOnlineManagement" -notin $Modules) {
Write-Host "Loading Exchange Online Management module"
Connect-ExchangeOnline -SkipLoadingCmdletHelp
}
Write-Host "Searching for failed login audit records..."
[array]$Records = Search-UnifiedAuditLog -StartDate (Get-Date).AddDays(-$LookbackDays) -EndDate (Get-Date).AddDays(+1) `
-Operations UserLoginFailed -SessionCommand ReturnLargeSet -ResultSize 5000 -Formatted
If ($Records.Count -eq 0) {
Write-Host "No audit records found for failed logins."
break
}
# Remove any duplicates and sort by date
$Records = $Records | Sort-Object Identity -Unique | Sort-Object { $_.CreationDate -as [datetime]} -Descending
Write-Host "Processing" $Records.Count "audit records..."
$Report = [System.Collections.Generic.List[Object]]::new()
ForEach ($Rec in $Records) {
$AuditData = ConvertFrom-Json $Rec.Auditdata
$ReportLine = [PSCustomObject]@{
TimeStamp = $Rec.CreationDate
User = $AuditData.UserId
Action = $AuditData.Operation
Status = $AuditData.ResultStatus
IpAddress = $AuditData.ActorIpAddress
Error = $AuditData.LogonError
UserAgent = $AuditData.ExtendedProperties.value[0]
}
$Report.Add($ReportLine)
}
$Report | Sort-Object User, Timestamp | Select-Object Timestamp, User, IpAddress, UserAgent | Out-GridView

Parameters

ParameterDefaultNotes
-LookbackDays90Number of days back to search the unified audit log.
Attribution