What It Does
The script is read-only. It finds the user accounts locked out right now, or, when AccountName is set, looks at only those users, and for each one reports the available lockout and failed sign-in evidence. It ends ALERT when any looked-at account is locked out, and OK otherwise. A user named in AccountName who is not locked out is still listed, with LockedOut false, so you can see recent failures before the lockout.
SourceComputer is the caller computer name of the newest lockout event 4740 for that user in the Security log of this domain controller and, if it is a different computer and reachable, of the PDC emulator. FailedAttempts and FailureSources come from the recent failed sign-ins for that user, events 4771 (Kerberos pre-authentication failed), 4776 (NTLM password check failed) and 4625 (failed logon), with the computer name or IP address each came from and how many times, up to the five most common.
Check the effective audit policy on the host expected to log each event. Event 4740 uses Audit User Account Management. The failure events use the applicable Kerberos Authentication Service, Credential Validation, Logon or Account Lockout audit subcategories. Run it on a domain controller, or on a member server, in which case only the PDC emulator's log is read. It needs the ActiveDirectory module (RSAT); if that is missing it ends ERROR, and on a computer that is not in a domain it ends SKIPPED.
After the owner corrects the offending task, service or saved credential, follow the client’s authorized account recovery procedure.
Before You Run It
- Run it on a domain controller, or on a member server that has the AD module.
- Check that the runner can read Security logs on the relevant domain controllers and that retention covers the incident.
- Use AccountName and a bigger LookbackHours when a user says it keeps happening.
- Remember the events come from this domain controller and the PDC emulator, not from every domain controller.
- Record the reported lockout time and time zone. Compare events in UTC and keep the account scoped to the client’s domain.
Worked Ticket: Repeat Lockout at One Client
Fictional ticket HD-2481. At a client’s finance office, mrivera locks out shortly after the 17:00 UTC export starts on two successive days. On the second day, run the read-only monitor during the lockout from an authorized, elevated Windows PowerShell session on the relevant domain controller, or as SYSTEM from the client’s RMM. Set AccountName=mrivera and LookbackHours=48. Use OutputFormat=Text for the result line or Json for the full row and warnings.
RESULT: ALERT | 1 account(s) locked out: mrivera (source FIN-JOB-02, 3 failed attempts)
JSON row fields from the same example include "SourceComputer":"FIN-JOB-02", "FailedAttempts":3 and "FailureSources":"FIN-JOB-02 (2)". The third matching failure record has an empty source. FailedAttempts counts matching records from the logs read, which may cover the same attempt more than once. FailureSources shows up to five named groups. A count by itself can’t establish the cause.
Confirm the Device and the Task Owner
Open the domain controller’s Security event 4740 for the affected account. Record the event time in UTC, the controller, event record ID and Caller Computer Name. Check both reported lockout times. Use the client’s inventory to identify FIN-JOB-02. For a VPN server, gateway or other intermediary, follow its approved logs to the originating session or device.
On the system where the logon failed, look for event 4625 just before the lockout and match the account and domain. Read Logon Type, Status and SubStatus, plus the process, workstation or address if present. In this ticket, FIN-JOB-02 records type 4 (Batch) and SubStatus 0xC000006A (incorrect password). A locked-account status of 0xC0000234 shows a blocked attempt, which can occur after the triggering failures.
The finance application owner confirms that the daily export task runs as the affected account and that its recorded start and failure times align with both lockouts. Get that owner to verify the task path, run-as identity and dependencies before authorizing a credential correction through the client’s approved secret-management process. Keep credentials out of the ticket. If the evidence points to unexpected devices or several targeted accounts, route the case to the security owner.
When the Report Has Gaps
A blank SourceComputer means the newest matching 4740 had no caller, or the query found no matching 4740. Read the Notes and warnings. A Security log read warning indicates a coverage gap. Check the time window, retention, effective audit policy and authorized remote log access. On a member server the script reads the PDC emulator’s Security log. On a domain controller it also reads the local Security log. Look at other relevant controllers or the client’s existing log collector when those logs miss the incident.
The monitor reads up to 5,000 events per query before filtering for the named account. On a busy controller, use the narrowest window that covers the incident and check the time directly in Event Viewer or your log collector. Zero FailedAttempts means no matching failure records were returned from those queries. Check the affected host’s own Security log for 4625 and the relevant DC for 4771 or 4776.
Verify the Fix
After the owner’s approved change, follow the client’s unlock procedure if the account is still locked. Have the user confirm normal sign-in. Have the task owner confirm the next scheduled export succeeds. Rerun with AccountName=mrivera and compare new events after the change time across the next scheduled cycle. An OK result describes the current lock state. Older failures and the previous caller can still appear within the lookback.
RESULT: OK | Not locked out: mrivera
Microsoft Event References
Event 4740: A user account was locked out
Event 4625: An account failed to log on
Type and Risk
- Type: Monitor. It checks one condition. It exits 1 when it finds the problem, so your RMM can alert on it, 0 when all is well, and 2 when it could not run.
- Risk: Low. It reads and changes nothing.
- Restart: It never restarts the machine.
- Typical run: Under 1 minute.
- Example answer: "1 account(s) locked out: jsmith (source WS-FIN-07, 12 failed attempts)"
- Tested before listing on a Windows Server 2025 domain controller (VM) with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- AccountName: Only look at these users, separated by commas or semicolons: the sign-in name (jsmith), DOMAIN\jsmith, or the user principal name. Default empty: every locked-out user. (Named AccountName, not UserName: Windows already has an environment variable called USERNAME, which the runtime would read.)
- LookbackHours: How far back to read events. Whole number, 1 to 720. Default 24.
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.