Export users from multiple groups and get user properties

This topic contains 8 replies, has 4 voices, and was last updated by  Windows LiveUser150 2 years, 2 months ago.

  • Author
  • #37766

    Windows LiveUser150

    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

  • #37772

    random commandline

    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 
  • #37783

    Windows LiveUser150

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

    know what groups i want to export users from?

  • #37785

    Richard Diphoorn

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

  • #37786

    Windows LiveUser150

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

  • #37859

    Windows LiveUser150


  • #37860

    David Schmidtberger

    @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.

  • #37868

    Windows LiveUser150

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

  • #37955

    Windows LiveUser150

    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


You must be logged in to reply to this topic.