AD Powershell Query

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.
Vincent DAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Prashant GirennavarCommented:
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.
Bob McCoyCommented:
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.
nashiookaCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.