Author Posts

April 14, 2016 at 7:06 am

This script works for one group. How can i get this to except multiple groups?

Get-ADGroupMember "ADGroup" -Recursive | select name | export-csv C:\Group.txt -NoTypeInformation -Append

$a = Get-Content C:\Group.txt

$a | Foreach-Object {$_ -replace '"', "}| Out-File C:\Group.txt

Get-Content C:\Group.txt | Get-ADUser -Properties * | Select-Object name,Surname,GivenName,emailaddress,distinguishedname | Export-Csv c:\Group.csv -notypeInformation

April 14, 2016 at 8:18 am

I don't have access to my Active Directory environment to test, but something like this should work.

$groups = get-adgroup -Filter * -Properties members
$groups.members | ForEach-Object {
Get-ADUser $_ -Properties * | Select-Object name,
Surname,GivenName,emailaddress,distinguishedname} |
Export-Csv C:\groups.csv -NoTypeInformation 

April 14, 2016 at 10:31 am

how does
$groups = get-adgroup -Filter * -Properties members

know what groups i want to export users from?

April 14, 2016 at 10:37 am

@Windows LiveUser150: Do you already have a textfile or CSV in where you have your groups specified?

April 14, 2016 at 10:38 am

yes. its about 10 so i just did it manually.

April 15, 2016 at 11:20 am

@Windows LiveUser150:
the code posted by random commandline would be running through all groups.

for your use-case, since you already seem familiar with get content, utilizing a text document listing the groups you want, i would do something along the lines of

$grouplist = get-content c:\groupstocheck.txt
foreach ($group in $grouplist)
{
get-adgroupmember $group|get-aduser -properties mail| Select-Object name,Surname,GivenName,mail,distinguishedname | Export-Csv c:\Group.csv -notypeInformation -append
}

obviously you could also use a CSV as an imput file and do import-csv instead but that changes a few things.

April 15, 2016 at 1:03 pm

thanks for answering. I think i tried that but will give a shot

April 18, 2016 at 11:12 am

That worked David
At first i didnt have the -append and was only getting the last group in the list. I guess i didnt get it when i copied

Thanks