Tracking Failed Logons in Active Directory with PowerShell

Turn on the right audit subcategories, find the event IDs that matter on member servers and domain controllers, and use PowerShell to trace failed logons and lockouts back to their source.

dns WindowsVPS Team calendar_today schedule5 min read
Tracking Failed Logons in Active Directory with PowerShell
tocOn this page 7 sections
  1. Step 1: Turn on the right audit subcategories
  2. Step 2: Make the Security log big enough
  3. Step 3: Know which events to read
  4. Step 4: Summarise failures with PowerShell
  5. Step 5: Check every domain controller
  6. Step 6: Trace a lockout to its source
  7. Make it routine

A failed logon could be a user who mistyped a password. It could also be a scheduled task still using an old password, or a botnet trying passwords against your RDP port. Active Directory records all three, but only when auditing is turned on, and the records are spread across several event IDs and machines. This guide covers how to turn on the right auditing, which events to look for, and how to pull useful answers out of them with PowerShell over an RDP session on your VPS.

Step 1: Turn on the right audit subcategories

Use the Advanced Audit Policy rather than the nine legacy categories. It lets you choose exactly what gets logged. Open Group Policy Management, edit the policy linked to the Domain Controllers OU, and go to Computer Configuration > Policies > Windows Settings > Security Settings > Advanced Audit Policy Configuration. Enable Failure auditing for these subcategories (Success too if you want a complete picture):

  • Account Logon > Credential Validation: NTLM checks handled by a domain controller.
  • Account Logon > Kerberos Authentication Service: Kerberos ticket requests, including bad passwords.
  • Logon/Logoff > Logon: failed logons on the machine the user tried to reach.
  • Account Management > User Account Management: Success, so that lockouts get recorded.

Apply the same Logon subcategory to your member servers through a policy linked to their OU. If you mix legacy and advanced settings, also enable Audit: Force audit policy subcategory settings to override audit policy category settings. Otherwise the legacy settings can quietly override yours.

Once the policy has applied, check from an elevated PowerShell prompt on the server:

gpupdate /force
auditpol /get /category:"Account Logon","Logon/Logoff"

On a standalone test box, or while you wait for the GPO, you can set a subcategory directly. The domain policy will overwrite this at the next refresh:

auditpol /set /subcategory:"Credential Validation" /failure:enable

Step 2: Make the Security log big enough

The default Security log size fills quickly on a domain controller, and even faster on a VPS with RDP open to the internet. When the log is too small, the evidence is overwritten before you look for it. Raise the maximum size under Event Log in the same GPO, or on one server:

wevtutil sl Security /ms:1073741824
wevtutil gl Security

For long-term retention, export the log on a schedule and send the files off the server. The approach in our Rclone guide for Windows Server also works for archived .evtx files.

Step 3: Know which events to read

On the server being logged into

Event 4625 means an account failed to log on. The useful fields are TargetUserName, IpAddress, WorkstationName, LogonType and SubStatus. Logon type 10 is an interactive RDP logon. Logon type 3 is a network logon, and with Network Level Authentication enabled, RDP brute-force attempts usually show up as type 3.

On domain controllers

  • 4771: Kerberos pre-authentication failed. Failure code 0x18 means a wrong password.
  • 4776: the DC tried to validate NTLM credentials. A non-zero error code means it failed.
  • 4740: an account was locked out. It names the computer the bad attempts came from.

Status codes worth memorising

  • 0xC000006A: correct user name, wrong password.
  • 0xC0000064: the user name doesn't exist. Many of these usually means someone is guessing names.
  • 0xC0000234: the account is already locked out.
  • 0xC0000072: the account is disabled.
  • 0xC0000071 / 0xC0000193: password expired / account expired.

Step 4: Summarise failures with PowerShell

Scrolling Event Viewer over RDP is slow. This script reads the last 24 hours of 4625 events and groups them by user and source address, so the noisiest sources are at the top:

$since = (Get-Date).AddHours(-24)
Get-WinEvent -FilterHashtable @{LogName='Security'; Id=4625; StartTime=$since} -ErrorAction SilentlyContinue |
  ForEach-Object {
    $evt  = $_
    $data = @{}
    ([xml]$evt.ToXml()).Event.EventData.Data | ForEach-Object { $data[$_.Name] = $_.'#text' }
    [pscustomobject]@{
      Time      = $evt.TimeCreated
      User      = $data.TargetUserName
      Source    = $data.IpAddress
      LogonType = $data.LogonType
      SubStatus = $data.SubStatus
    }
  } |
  Group-Object User, Source |
  Sort-Object Count -Descending |
  Select-Object Count, Name -First 20

Many different user names coming from one public IP is a password-spraying or brute-force attempt. In that case, block the address and reduce the attack surface instead of resetting passwords. A domain controller should not accept RDP from the internet at all. Put it behind a firewall allow-list or a VPN, and give users a separate session host. Our Remote Desktop VPS overview covers that setup.

Step 5: Check every domain controller

Each DC logs only the authentications it handled, so if you check just one you will miss some. Run this from any server that has the RSAT AD module installed:

$since = (Get-Date).AddHours(-24)
foreach ($dc in (Get-ADDomainController -Filter *).HostName) {
  $count = (Get-WinEvent -ComputerName $dc -FilterHashtable @{LogName='Security'; Id=4771,4776; StartTime=$since} -ErrorAction SilentlyContinue).Count
  [pscustomobject]@{ DC = $dc; Events = $count }
}

The 4776 count includes successful validations too. To see only failures, filter on events whose error code isn't 0x0.

Step 6: Trace a lockout to its source

When an account keeps getting locked out, check the PDC emulator first. Bad-password attempts are forwarded to it, and it usually logs the 4740:

$pdc = (Get-ADDomain).PDCEmulator
Get-WinEvent -ComputerName $pdc -FilterHashtable @{LogName='Security'; Id=4740} -MaxEvents 25 |
  Select-Object TimeCreated,
    @{n='User';           e={$_.Properties[0].Value}},
    @{n='CallerComputer'; e={$_.Properties[1].Value}}

Then go to the caller computer and look for whatever is still using the old password: a mapped drive, a service account, a scheduled task, a saved RDP credential, or a disconnected session that was never logged off. Stale credentials are the most common cause of repeated lockouts.

Make it routine

Save the summary script as a .ps1 file and run it every morning from Task Scheduler, writing the results to a CSV file or sending them by email. If it shares a busy server with other workloads, our guide on setting scheduled task priority shows how to keep a heavy log query from slowing down production. A daily look at failed logons is cheap, and it shows attacks and misconfigured services early, while they are still easy to fix.

Run this on your own Windows VPS

Get a Windows VPS from WindowsVPS in about 60 seconds, with full Administrator access over RDP, NVMe storage and the Windows license included.

Buy a Windows VPS →

dns

Written by the WindowsVPS Team

We run Windows Server VPS hosting and write these guides from the same setups our customers use: RDP, PowerShell, trading terminals and always-on bots. Questions about a guide? Contact us.

About WindowsVPS Windows VPS guides All articles

Ready to deploy your server?

Windows or Linux VPS — up and running in under 60 seconds.