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 /home/deliveron/wwwroot/packages/problog/helpers/blogify.php on line 370 Call Stack: 0.0007 625704 1. {main}() /home/deliveron/wwwroot/index.php:0 0.0011 626152 2. require('/home/deliveron/wwwroot/concrete/dispatcher.php') /home/deliveron/wwwroot/index.php:2 0.0033 628200 3. require('/home/deliveron/wwwroot/concrete/startup/updated_core_check.php') /home/deliveron/wwwroot/concrete/dispatcher.php:20 0.0034 628912 4. require('/home/deliveron/wwwroot/updates/concrete5.6.2.1_updater/concrete/dispatcher.php') /home/deliveron/wwwroot/concrete/startup/updated_core_check.php:6 0.3667 5469704 5. Concrete5_Library_View->render() /home/deliveron/wwwroot/updates/concrete5.6.2.1_updater/concrete/dispatcher.php:264 2.0683 10605680 6. include('/home/deliveron/wwwroot/packages/problog/page_types/pb_post.php') /home/deliveron/wwwroot/updates/concrete5.6.2.1_updater/concrete/core/libraries/view.php:945 2.0892 10807632 7. Concrete5_Model_Area->display() /home/deliveron/wwwroot/packages/problog/page_types/pb_post.php:19 2.1014 10852216 8. Concrete5_Library_BlockView->render() /home/deliveron/wwwroot/updates/concrete5.6.2.1_updater/concrete/core/models/area.php:656 2.1053 10909736 9. include('/home/deliveron/wwwroot/packages/problog/blocks/content/templates/blog_post/view.php') /home/deliveron/wwwroot/updates/concrete5.6.2.1_updater/concrete/core/libraries/block_view.php:311 2.2199 11741752 10. BlogifyHelper->getBlogAuthorInfo() /home/deliveron/wwwroot/packages/problog/blocks/content/templates/blog_post/view.php:67