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 7 months, 2 weeks ago.

  • Author
    Posts
  • #37766
    Profile photo of Windows LiveUser150
    Windows LiveUser150
    Participant

    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
    Profile photo of Windows LiveUser150
    Windows LiveUser150
    Participant

    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
    Profile photo of Windows LiveUser150
    Windows LiveUser150
    Participant

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

  • #37859
    Profile photo of Windows LiveUser150
    Windows LiveUser150
    Participant

    poke

  • #37860
    Profile photo of David Schmidtberger
    David Schmidtberger
    Participant

    @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
    Profile photo of Windows LiveUser150
    Windows LiveUser150
    Participant

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

  • #37955
    Profile photo of Windows LiveUser150
    Windows LiveUser150
    Participant

    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

You must be logged in to reply to this topic.