# CodeSOD: An Odd Sort

DevFeed: [CodeSOD: An Odd Sort](<https://devfeed.tech/articles/codesod-an-odd-sort-28504.md>)

Original publisher: [Read original article](<https://thedailywtf.com/articles/an-odd-sort>)

Author: Remy Porter

Published: 2026-09-15T06:30:00Z

Content type: opinion

Language: en

Sources: [The Daily WTF](<https://devfeed.tech/sources/the-daily-wtf.md>)

Topics: [Code](<https://devfeed.tech/topics/code.md>), [PowerShell](<https://devfeed.tech/topics/powershell.md>), [Script](<https://devfeed.tech/topics/script.md>), [Sorting](<https://devfeed.tech/topics/sorting.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [codesod](<https://devfeed.tech/tags/codesod.md>), [csv](<https://devfeed.tech/tags/csv.md>), [excel](<https://devfeed.tech/tags/excel.md>), [powershell](<https://devfeed.tech/tags/powershell.md>), [report](<https://devfeed.tech/tags/report.md>), [script](<https://devfeed.tech/tags/script.md>), [sorting](<https://devfeed.tech/tags/sorting.md>)

## AI overview

The article critiques a PowerShell script that queries Active Directory for users and their last logon times. It explains that the script's alphabet-based approach does not correctly sort names, performs unnecessary searches and property loading, and generates a CSV report for Excel despite these inefficiencies.

## Source excerpt

Let's say we wanted to query Active Directory and print out a report of all of our users, and their last logon time. That seems like a pretty normal task for a Powershell script. It'd probably be short and easy to read, at least if it were written by a normal person. Alice sends us one that wasn't. She's already done us a favor, as she writes: "Code cleaned up and indented for the whitespace-missing-impaired." ##################################### # lists accounts and selected attributes alphabetically ##################################### foreach( $letter in "a", "b", "c"......"z") { $strfilter = $letter + "*" $objdomain = New-object System.DirectoryServices.DirectoryEntry $objSearcher = New-object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objdomain $objSearcher.Filter = $strFilter $objSearcher.PropertiesToLoad.Add("name"); $colResults = $objSearcher.FindAll() foreach($result in $colResults) { $name = $result.Properties.Name $searcher = New-Object DirectoryServices.DirectorySearcher([adsi]"") $searcher.filter "(&(objectCategory=User)(sAMAccountName=$name))" $users = searcher.FindAll() foreach($user in $users) { Write-Output $user.properties.item("name") + "," + $user.properties.item("lastLogon") } } } This accomplishes sorting alphabetically by iterating across the alphabet. Which, I suspect, isn't going to actually get them in alphabetical order; it makes sure that albert and alice appear before bob, but doesn't enforce that albert must come before alice. In any case, we iterate across the alphabet, and then create a searcher that finds a*, then b*, etc. We explicitly tell the searcher that the only property we care about is the name field, so that we don't load unnecessary fields, like the ones we want to report on. We then iterate across the list of names, construct a new searcher, and search for the account with the username we fetched. That lets us get all of the fields we need, including the ones we aren't going to use. Now, we sea