PowerTip: Use a Single Line PowerShell Command to List All Users in an OU

PowerTip: Use a Single Line PowerShell Command to List All Users in an OU

  • Comments 11
  • Likes

Summary: Use a single line Windows PowerShell command to list all users in an OU.

Hey, Scripting Guy! Question How can I list all users in a particular organizational unit (OU)?

      Hey, Scripting Guy! Answer Use the Get-ADUser cmdlet from the ActiveDirectory Module (available from the RSAT tools). Specify the SearchBase as the name of the OU, and use a wildcard pattern for the Filter.

                                    Get-ADUser -Filter * -SearchBase "ou=testou,dc=iammred,dc=net"

                       Note In Windows PowerShell 3.0, you do not have to load a module prior to using it.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Used that one just last week, I had to check the number of users in a OU. You can use: (Get-ADUser -Filter * -SearchBase "ou=testou,dc=iammred,dc=net").Count

  • Additional,

    If don't want list the sub OU in target OU, can use -SearchScope parameter.

  • @Nico Martens that is a good tip, and a great way to show how to expand the code.

    @Larry Song CN yes, the SearchScope is a great way to control searching. Thank you for sharing.

  • Thx bro

  • The term 'Get-ADUser' is not recognized as the name of a cmdlet...
    + Get-ADUser < -filter="" *="">

  • I have Powershell 3.0 on a Windows 7 SP1 PC and the Get-ADUser is not a recongized cmdlet. Any help?

  • I have Powershell 3.0 on a Windows 7 SP1 PC and the Get-ADUser is not a recongized cmdlet. Any help?

  • Phil Reynolds / NoPhilter;

    Install the Remote Server Administration Tools on your system; it's not a server/domain controller if it is Windows 7 - so you don't have the right modules. :-)

  • This power shell command will give you all AD users and put them in a file on C:\temp folder.
    Enjoy

    Get-ADUser -searchbase “ou=specialusers,ou=users,dc=mydomain,dc=com” -filter * -Properties Department | Select-Object name, department | export-csv c:\temp\myfile.csv

  • I want to display active users in an OU

  • To get enabled / disabled users

    Get-ADUser -f * -pr * -searchbase $adlocation | where {$_.enabled -eq "Enabled"} | select name, lastlogindate

    where $adlocation is the FQDN of the OU.