Remy Porter published this CodeSOD item in The Daily WTF on 15 September 2026. Alice submitted a PowerShell script for a routine Active Directory report that prints each user’s name and last logon time. She presented the listing as cleaned up and indented.
The script walks from a through z. For each letter, it creates a `System.DirectoryServices.DirectoryEntry` and a `System.DirectoryServices.DirectorySearcher`, filters names with that initial, requests the `name` property, and processes the results. It then creates another `DirectorySearcher` for each returned name, searches for `objectCategory=User` with the matching `sAMAccountName`, and outputs `name` together with `lastLogon`.
The letter loop groups entries by initial. Their order inside each group remains undefined, so `albert` and `alice` are not guaranteed to appear in sequence. The first query loads only `name`. The second retrieves account data, while the output uses two fields. For a lookup expected to return one account, the script loops over `FindAll()`. `FindOne()` matches that search target. A manager relies on the resulting CSV for Excel, making the script mission critical.




Comments
No comments yet — be the first.
Open the discussion
No account or password needed — just enter your e-mail and we’ll send you a one-time sign-in link. First time here? You’re set up automatically.
Your rating will be applied automatically after you sign in.
Check your inbox
We’ve sent a sign-in link to …. Open it on this device — this tab will sign you in automatically.
Nothing arrived? Check your spam folder — and mark the mail as "Not spam" so it lands in your inbox next time.