Link to home
Create AccountLog in
Avatar of cawasaki
cawasaki

asked on

powerhsell script to export all group membership of some users

hello,

i need help for a script can get from a csv file contain a list of user like this:

john.smith
mark.rozenberg
.....

and for every user get all group he is member (security group and distribution group) from active directory and put all on a csv file i can use and import it on excel file

thank you
Avatar of oBdA
oBdA

If the input file looks like in your description, it's not a csv, it's a plain text file. If it actually is a csv, you need to provide the header row with the column names.
Assuming the names you listed are the SamAccountNames, you can use the following:
Get-Content -Path C:\Temp\users.txt | Get-ADUser -Property memberOf | ForEach-Object {
	$adUser = $_
	$adUser.memberOf | Get-ADGroup | Select-Object -Property @{n='User'; e={$adUser.SamAccountName}}, Name, GroupCategory, GroupScope, SamAccountName, DistinguishedName
} | Export-Csv -NoTypeInformation -Path C:\Temp\Groups.csv

Open in new window

Avatar of cawasaki

ASKER

hello oBdA


just a small error on "Export-Csv -NoTypeInformation -Patrh"

the script work well, if i need to see recursive group possible or hard to do?

for exemple if user member of group 1 and group 1 is member of other groups?

thanks
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer