Home » PowerShell » Get AdUser Sort by SAMAccountname

Get AdUser Sort by SAMAccountname

To get aduser sort by samaccountname from the active directory, use the Get-AdUser cmdlet to get a list of all users and pipe aduser objects to the Sort-Object command to sort by samaccountname.

The Get-AdUser cmdlet has the SAMAccountName user naming attribute in the active directory. It stores the user logon name or user object.

In this article, we will discuss how to get aduser sort by samaccountname using the Get-AdUser cmdlet in PowerShell.

Get AdUser Sort by SAMAccountname

Use the Get-AdUser cmdlet to get one or more active directory users and pipe the aduser objects to the Sort-Object to sort aduser by samaccountname.

 Get-AdUser -Filter * -Property samaccountname | Sort-Object -Property samaccountname | Format-Table Name,samaccountname, Surname -Wrap

In the above PowerShell script, the Get-AdUser uses the Filter * parameter to get all the adusers and uses the Property parameter to select samaccountname.

It passes the aduser objects to the Sort-Object command to sort by samaccountname attribute.

The Sort-Object sorts the aduser by samaccountname and pipes the results to the Format-Table to display a list of adusers in table format.

The output of the above PowerShell script to sort aduser by samaccountname is:

Get AdUser sort by samaccountname
Get AdUser sort by samaccountname

In the output, it can be seen that the adusers are sorted by samaccountname.

PS C:\> Get-AdUser -Filter * -Property samaccountname | Sort-Object -Property samaccountname | Format-Table Name,samaccountname, Surname -Wrap

Name          samaccountname Surname
----          -------------- -------
adam          adam           Strauss
Aron Seth     arons          Seth
Chris Dore    chrisd         Dore
Dave NewHouse Dev.NewHouse   NewHouse
Don           Don            Astle
Erick Jones   ErickJ         Jones
Esh Deol      EshD           Deol
Gary Willy    garyw          Willy
Guest         Guest
Harsh Dev     Harsh.Dev      Dev
krbtgt        krbtgt
masadmin      masadmin       Paul
nathan        nathan         Tim
Netya Xu      Netya.Xu       Xu
Tira Elsa     Tira.Elsa      Elsa
Tom Smith     toms           Smith


PS C:\>

Conclusion

I hope the above article on how to get aduser sort by samaccountname is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.