AD Powershell Query

Vincent D
Vincent D used Ask the Experts™
on
Hi ,

I am looking to use Powershell to list all users in certain AD security group as well as the users job title in AD. I need both list of users and their job title exported to csv.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
To extract users from security group try below command in powershell

Import-Module ActiveDirectory
Get-ADGroupMember -identity "Group Name" -server "domain name" | select name | export-csv c:\users.csv -notytpe

Open in new window


To get user account properties , try below command in powershell

import-module Activedirectory
import-csv c:\users.csv | %{get-aduser -identity $_ -server "domain name" -properties * | select SamaccountName,EmployeeID,EmployeeNumber,enabled,title,Manager,targetAddress,officephone,TelephoneNumber,ipphone,mobile,mobilephone,description,department,DistinguishedName,displayname} | Export-csv C:\userdetails.csv -notype

Open in new window


Please add GroupName and DomainName in the commandline and run the script

Thanks,

-Prashant Girennavar.
If you are ruuning PS V3.0 or later, you don't have to import the AD module.  

To put it all together you would do something like this.

$userList = Get-ADGroupMember -Identity MyGroupName
$results = foreach ($user in $userList) {
    $userInfo = Get-ADUser -Identity $user -Properties Title
    [PSCustomObject]@{
        Name = $userInfo.Name
        Title = $userInfo.Title
    }
}
$results | Export-Csv -Path C:\ephemeral\users.csv -NoTypeInformation

Open in new window

Of course modify the group name on the first line to meet your needs.  And I am going off your original stated requirement that you only want their name and title.
It's best when the particular module offers native PoSh capabilities.  Get-ADGroupMember can pipe directly to Get-ADUser so something like below should work fine and is concise.  There's no need for looping, pre-exporting or custom objects.  Just adjust your Select statement for additional properties and/or customization of output objects.

Get-ADGroupMember <GroupName> | 
Get-ADUser -Properties "title" | 
Select Name,Title | 
Export-Csv -Path <PathToCSV_Output> -NoTypeInformatio

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial