Export users from multiple groups and get user properties

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of Windows LiveUser150 Windows LiveUser150 5 months, 1 week ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #37766

    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
    Profile photo of random commandline
    random commandline
    Participant

    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

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

    know what groups i want to export users from?

    #37785
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

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

    #37786

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

    #37859

    poke

    #37860

    @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

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

    #37955

    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

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.