Query for User Accounts in Active Directory with PowerShell

Feb 24, 2012

Occasionally there is a need to quickly query Active Directory for all user accounts or user accounts with only certain values in particular properties. This can be done by installing and loading the Microsoft Active Directory Administration module for PowerShell. This is an add-on module, named ActiveDirectory, that provides cmdlets that let you manage your Active Directory domains.

After you install the ActiveDirectory module, there is now a new PowerShell option in Administrative Tools, called “Active Directory Module for Windows PowerShell”:


This brings up the bland, DOS-like command prompt with the ActiveDirectory module automatically loaded. But what if you like to work in the Windows PowerShell Integrated Scripting Environment (ISE)? Start up ISE and then run the following command:

Import-module ActiveDirectory

That will load the ActiveDirectory module into your ISE session, so that you can use the desired cmdlets.

Querying for User Accounts

To query for user accounts, use the Get-ADUser cmdlet. For example, here is how you would query against your domain for all user accounts:

Get-ADUser -Filter*-SearchBase "DC=ad,DC=company,DC=com"

If you wanted to query for all of the user accounts with the last name “Collicott”, you would run the following:

Get-ADUser -Filter {Surname -eq"Collicott"} -SearchBase "DC=ad,DC=company,DC=com"

To export the e-mail addresses for all user accounts to a CSV file, you could run the following:

Get-ADUser -Filter*-SearchBase "DC=ad,DC=company,DC=com"-Properties mail | Select mail | Export-CSV "Email Addresses.csv"

You can also find additional examples by viewing the help on the cmdlet:

Get-Help Get-ADUser -examples

Tags: powershell 2.0
Category: PowerShell
Fatal error: Call to a member function getUserUserBio() on a non-object in /mnt/HostVol01/deliveron/wwwroot/packages/problog/helpers/blogify.php on line 370 Call Stack: 0.0003 625712 1. {main}() /mnt/HostVol01/deliveron/wwwroot/index.php:0 0.0005 626168 2. require('/mnt/HostVol01/deliveron/wwwroot/concrete/dispatcher.php') /mnt/HostVol01/deliveron/wwwroot/index.php:2 0.0014 628248 3. require('/mnt/HostVol01/deliveron/wwwroot/concrete/startup/updated_core_check.php') /mnt/HostVol01/deliveron/wwwroot/concrete/dispatcher.php:20 0.0016 628968 4. require('/mnt/HostVol01/deliveron/wwwroot/updates/concrete5.6.2.1_updater/concrete/dispatcher.php') /mnt/HostVol01/deliveron/wwwroot/concrete/startup/updated_core_check.php:6 0.1404 5472664 5. Concrete5_Library_View->render() /mnt/HostVol01/deliveron/wwwroot/updates/concrete5.6.2.1_updater/concrete/dispatcher.php:264 1.0123 10699448 6. include('/mnt/HostVol01/deliveron/wwwroot/packages/problog/page_types/pb_post.php') /mnt/HostVol01/deliveron/wwwroot/updates/concrete5.6.2.1_updater/concrete/core/libraries/view.php:945 1.0219 10925864 7. Concrete5_Model_Area->display() /mnt/HostVol01/deliveron/wwwroot/packages/problog/page_types/pb_post.php:19 1.0282 10970496 8. Concrete5_Library_BlockView->render() /mnt/HostVol01/deliveron/wwwroot/updates/concrete5.6.2.1_updater/concrete/core/models/area.php:656 1.0323 11028080 9. include('/mnt/HostVol01/deliveron/wwwroot/packages/problog/blocks/content/templates/blog_post/view.php') /mnt/HostVol01/deliveron/wwwroot/updates/concrete5.6.2.1_updater/concrete/core/libraries/block_view.php:311 1.0770 11860320 10. BlogifyHelper->getBlogAuthorInfo() /mnt/HostVol01/deliveron/wwwroot/packages/problog/blocks/content/templates/blog_post/view.php:67