To list adusers from the active directory, you can use the PowerShell Get-AdUser cmdlet, net user, gwmi method, and command-line dsquery tool.
Using these commands in PowerShell, you can get a list of adusers, their name, samaccountname, and domain.
To get a user list from the local machine or active directory, PowerShell provides cmdlets like the net user, Get-AdUser, dsquery, etc…
In this article, we will discuss how to list adusers from the active directory using the PowerShell commands Get-AdUser, net user, and gwmi.
PowerShell List AdUsers using the Get-AdUser
In PowerShell, to list adusers from the active directory, use the Get-AdUser cmdlet.
The Get-AdUser gets one or more active directory users and their properties like samaccountname, distinguishedname, etc…
Get-AdUser -Filter * | Select Name
In the above PowerShell script, the Get-AdUser cmdlet uses the Filter * parameter to get the user list.
The output of the above script in PowerShell to list adusers from the active directory is:
PowerShell User List using net user
Using the net user command, you can get the user list in PowerShell.
net user
In the above command line, the net user command gets the user list from the local machine.
The output of the above net user command in cmd or PowerShell to get the user list is:
Get a List of Users using WMI Method
Using the WMI method, you can retrieve the user list.
gwmi win32_UserAccount | Select Name, FullName, Caption, Domain
In the above PowerShell script, it uses the WMI method class win32_UserAccount to retrieve the user list and display their name, domain, etc…
The output of the above script to list users in PowerShell is:
PS C:\> gwmi win32_UserAccount | Select Name, FullName, Caption, Domain
Name FullName Caption Domain
---- -------- ------- ------
Guest SHELLPRO\Guest SHELLPRO
krbtgt SHELLPRO\krbtgt SHELLPRO
toms Tom Smith SHELLPRO\toms SHELLPRO
ErickJ Erick Jones SHELLPRO\ErickJ SHELLPRO
garyw Gary Willy SHELLPRO\garyw SHELLPRO
chrisd Chris Dore SHELLPRO\chrisd SHELLPRO
adam Adam Strauss SHELLPRO\adam SHELLPRO
nathan Nathan Tim SHELLPRO\nathan SHELLPRO
Don Don Astle SHELLPRO\Don SHELLPRO
List AdUser using the Dsquery tool
Using the dsquery tool, you can query the active directory and gets the user objects and pipe to the dsget tool to get the user distinguishedname and display name.
dsquery user | dsget user -dn -display
The output of the above query list adusers as given below.
PS C:\> dsquery user | dsget user -dn -display
dn display
CN=Guest,CN=Users,DC=SHELLPRO,DC=LOCAL
CN=krbtgt,CN=Users,DC=SHELLPRO,DC=LOCAL
CN=Tom Smith,OU=SALES,DC=SHELLPRO,DC=LOCAL Tom Smith
CN=Erick Jones,OU=HR,DC=SHELLPRO,DC=LOCAL Erick Jones
CN=Gary Willy,OU=HR,DC=SHELLPRO,DC=LOCAL Gary Willy
CN=Chris Dore,OU=SALES,DC=SHELLPRO,DC=LOCAL Chris Dore
Conclusion
I hope the above article on how to list adusers using PowerShell cmdlets and command-line tools is helpful to you.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.