Link to home
Start Free TrialLog in
Avatar of Ron Shorts
Ron ShortsFlag for United States of America

asked on

Powershell script to output if user part of AD group/s

I have the below script that pulls from a list and outputs to a csv the samaccountname and mail attributes.

What I'm trying to do is next to each user check if each user is a member of a certain AD Group, or two AD Groups actually, for this example, the name of the two groups would be "GroupA" and "GroupB"

So the output would be, listing the name of the two groups I want to check in columns 3 and 4:

sAMAccountName     mail                                        GroupA                   GroupB  
--------------------------      -------                                       --------------               --------------
user1                            user1@domain.com           GroupA
user2                            user2@domain.com                                             GroupB


Assuming user1 is a member of GroupA and user2 is a member of GroupB

Import-Csv -Path C:\Temp\users.csv | ForEach-Object {
	Get-ADUser -Filter "(sAMAccountName -eq '$($_.sAMAccountName)')" -Properties sAMAccountName,mail  | Select-Object sAMAccountName,mail
} | Export-CSV mailusers.csv -NoTypeInfo

Open in new window



Thanks,

Ron
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ron Shorts

ASKER

AWESOME!  Thank you!!!