Back to Resources
Level
Script
Security
Failed login attempts can indicate unauthorized access attempts, brute-force attacks, misconfigured credentials, or forgotten passwords. Without real-time monitoring, organizations risk security breaches, account lockouts, or undetected threats. This script helps IT teams proactively detect and respond to suspicious authentication failures before they escalate into serious security incidents.
This script scans Windows Event Logs for failed login attempts (Event ID 4625) in the past hour and extracts the usernames associated with these failures. If any failed logins are detected, it outputs an alert listing the affected accounts. By integrating this script with a script-based monitor in Level, IT teams can receive real-time alerts whenever failed login attempts occur, allowing them to investigate and take appropriate action.
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-login-any-user
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
20if ($FailedLogins.Count -gt 0) {
21 $AlertMessage = "ALERT: Multiple failed login attempts detected in the last hour!`n"
22
23 # Extract usernames from the events
24 $FailedLogins | ForEach-Object {
25 $Xml = [xml]$_.ToXml()
26 $Account = $Xml.Event.EventData.Data | Where-Object { $_.Name -eq "TargetUserName" } | Select-Object -ExpandProperty "#text"
27
28 if ($Account) {
29 $AlertMessage += " - Failed login for account: $Account`n"
30 }
31 }
32
33 Write-Host $AlertMessage.Trim()
34 exit 1
35} else {
36 Write-Host "No failed login attempts detected."
37 exit 0
38}
39
Windows - Failed Login Attempt (Any User)
This PowerShell script monitors Windows security logs for failed login attempts within the last hour. When it detects failed logins (Event ID 4625), it extracts the targeted usernames from the event data and generates an alert message listing all accounts that experienced failed login attempts, providing a straightforward security monitoring solution.
PowerShell
300
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.