Back to Resources
Level
Script
Security
Repeated failed login attempts on administrator accounts can indicate brute-force attacks, unauthorized access attempts, or misconfigured credentials. Monitoring these failed logins in real-time helps IT professionals and MSPs quickly identify and mitigate potential security threats before they lead to a breach.
This script scans Windows Event Logs for failed login attempts (Event ID 4625) within the last hour and cross-references the failed attempts against a list of administrator accounts. If any failed login attempts are detected for admin users, the script generates an alert, allowing IT teams to investigate and respond proactively. By integrating this script with a script-based monitor in Level, organizations can receive real-time alerts whenever unauthorized admin login attempts occur.
Script
1<#
2This resource is provided as a convenience for Level users. We cannot
3guarantee it will work in all environments. Please test before deploying
4to your production environment. We welcome contributions to our community
5library
6
7# Level Library
8# https://level.io/library/script-windows-failed-admin-login
9#>
10
11$TimeFrame = (Get-Date).AddHours(-1)
12
13# Get failed logon attempts in the last hour
14$FailedLogins = Get-WinEvent -FilterHashtable @{
15 LogName = 'Security'
16 Id = 4625
17 StartTime = $TimeFrame
18} -ErrorAction SilentlyContinue
19
20# Get a list of administrator accounts
21$AdminAccounts = (Get-LocalGroupMember -Group "Administrators").Name -replace '^.*\\' # Extracts just usernames
22
23if ($FailedLogins.Count -gt 0) {
24 $AlertMessage = "ALERT: Failed admin login attempts detected in the last hour!`n"
25
26 # Extract usernames from the events
27 $FailedAdminLogins = $FailedLogins | ForEach-Object {
28 $Xml = [xml]$_.ToXml()
29 $Account = $Xml.Event.EventData.Data | Where-Object { $_.Name -eq "TargetUserName" } | Select-Object -ExpandProperty "#text"
30
31 if ($Account -and $AdminAccounts -contains $Account) {
32 $Account # Return only matching admin accounts
33 }
34 }
35
36 if ($FailedAdminLogins) {
37 $FailedAdminLogins | ForEach-Object { $AlertMessage += " - Failed login for admin account: $_`n" }
38 Write-Host $AlertMessage.Trim()
39 exit 1
40 }
41}
42
43Write-Host "No failed admin login attempts detected."
44exit 0
Windows - Failed Admin Login
This PowerShell script monitors Windows security logs for failed login attempts targeting administrator accounts within the last hour. It retrieves failed login events (Event ID 4625), cross-references them against the local Administrators group membership, and generates an alert message if any failed attempts are detected for admin accounts, providing a simple but effective security monitoring tool.
PowerShell
300
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.