Greg's Tech blog

Sorting AD Objects by OU

Wednesday 30 of September, 2020

Today I was trying to a list of of AD computer objects to group them by OU.  The obvious way was to try sorting by distinguishedname.  But sorting is simply alphabetical and so it is simply a sort by the computer name since that is listed first


Then I tried canonicalname and got results I could use


Here is the Powershell I used

get-adcomputer -filter * -properties operatingsystem,canonicalName|select name,operatingsystem,canonicalName,distinguishedname|sort canonicalName|ft

Note: You must specify "-properties canonicalname" to force the server to return the canonicalname property  so you can use it.