PowerShell Script to Get All AD Users Easily
Use the PowerShell command
Get-ADUser -Filter * to retrieve all Active Directory users in your domain.Examples
InputGet-ADUser -Filter *
OutputDistinguishedName : CN=John Doe,OU=Users,DC=example,DC=com
Name : John Doe
ObjectClass : user
...
InputGet-ADUser -Filter * | Select-Object Name, SamAccountName
OutputName SamAccountName
---- --------------
John Doe jdoe
Jane Smith jsmith
...
InputGet-ADUser -Filter * -Properties EmailAddress | Select-Object Name, EmailAddress
OutputName EmailAddress
---- ------------
jdoe john.doe@example.com
jsmith jane.smith@example.com
...
How to Think About It
To get all Active Directory users, you use the
Get-ADUser cmdlet with a filter that matches all users. The filter * means 'all'. You can then select which user properties to show.Algorithm
1
Open PowerShell with Active Directory module loaded.2
Run <code>Get-ADUser</code> with filter <code>*</code> to get all users.3
Optionally select specific user properties to display.4
Output the list of users.Code
powershell
Import-Module ActiveDirectory
$allUsers = Get-ADUser -Filter *
$allUsers | ForEach-Object { Write-Output $_.Name }Output
John Doe
Jane Smith
Alice Johnson
...
Dry Run
Let's trace getting all users and printing their names.
1
Run Get-ADUser -Filter *
Returns a list of user objects: [User1, User2, User3]
2
ForEach user, output the Name property
Prints: John Doe, Jane Smith, Alice Johnson
| User Object | Name Output |
|---|---|
| User1 | John Doe |
| User2 | Jane Smith |
| User3 | Alice Johnson |
Why This Works
Step 1: Import Active Directory Module
The Import-Module ActiveDirectory command loads the AD cmdlets needed to query users.
Step 2: Get All Users
The Get-ADUser -Filter * command fetches all user accounts from AD.
Step 3: Output User Names
Looping through each user object and printing Name shows readable user names.
Alternative Approaches
Using LDAP Filter
powershell
Get-ADUser -LDAPFilter "(objectClass=user)"This uses LDAP syntax to get all users; useful if you prefer LDAP filters.
Get Specific Properties
powershell
Get-ADUser -Filter * -Properties EmailAddress | Select-Object Name, EmailAddress
Fetches users with their email addresses for more detailed info.
Export to CSV
powershell
Get-ADUser -Filter * | Select-Object Name, SamAccountName | Export-Csv -Path users.csv -NoTypeInformation
Saves the user list to a CSV file for reporting or sharing.
Complexity: O(n) time, O(n) space
Time Complexity
The command queries all users once, so time grows linearly with the number of users.
Space Complexity
Storing all user objects requires memory proportional to the number of users.
Which Approach is Fastest?
Using -Filter * is straightforward and efficient; LDAP filters may be slightly faster in complex queries.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Get-ADUser -Filter * | O(n) | O(n) | Simple full user list |
| Get-ADUser -LDAPFilter | O(n) | O(n) | Advanced LDAP queries |
| Export to CSV | O(n) | O(n) | Saving data for reports |
Always run PowerShell as administrator and ensure the Active Directory module is installed before running AD commands.
Forgetting to import the Active Directory module or running PowerShell without proper permissions causes errors.