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


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.

Comments (18)

  1. Anonymous says:

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

  2. Anonymous says:

    Additional,

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

  3. WaseemAhsan says:

    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:tempmyfile.csv

  4. Nico Martens says:

    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

  5. Ed Wilson says:

    @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.

  6. Albeezzyy says:

    Thx bro

  7. Frustrated by the Shell says:

    The term ‘Get-ADUser’ is not recognized as the name of a cmdlet…
    + Get-ADUser <<<< -Filter * -SearchBase.....

  8. Phil Reynolds says:

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

  9. Shenan Stanley says:

    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. 🙂

  10. Daniel says:

    I want to display active users in an OU

  11. specise_8472 says:

    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.

  12. shawn says:

    You can omit "where" statement. Get-ADUser -f {Enabled -eq- $true} -pr * -searchbase $adlocation | select name, lastlogindate

  13. Charles Karabell says:

    Is it possible to list all users in the domain and their OU?

    For example:
    Domain.net users
    Admin 1, OU: admins
    User 2, OU: Users

    etc, etc

  14. Mike Compton says:

    How would I filter so that the only information that is given back to me is the SamAccountName and then pipe that SamAccountName for all users in an OU to a CSV file? Thank you!

  15. John Harris says:

    Mike C, Not sure if this is what you were looking, but this will get all users from an OU but not include sub folders and then export them to a .CSV File.

    Get-ADUser -Filter * -SearchScope OneLevel -SearchBase ‘OU=XXXXXX,DC=XXXXXX,DC=XXXXXX’ | select SamAcountName | Out-File C:UsersXXXXXXDesktopFileName.csv

    If you want to include all sub folders it will look something like this

    Get-ADUser -Filter * -SearchBase ‘OU=XXXXX,DC=XXXXXXX,DC=XXX’ | select SamAccountName | Out-File C:UsersXXXXXDesktopFileName.csv

  16. greg says:

    How to export to file of list users of OU.
    ex:
    OU= IT -user1,u2,u3….
    OU= DI – user11,u12,u13
    OU=DF -user21,u22,u23

    But in export file will we get on one line by line :
    OU= IT,user1,u2,u3….
    OU= DI, user11,u12,u13
    OU=DF,user21,u22,u23

  17. Martin says:

    Exactly what I was looking for. Thanks.

  18. Chet says:

    Is it possible to get the OU information for a list of users in a spreadsheet? This would really help.

    Thanks

    Chet

Skip to main content