Export members of multiple groups into single file

Welcome Forums General PowerShell Q&A Export members of multiple groups into single file

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #104777

    Participant
    Points: 0
    Rank: Member

    Hi,

    I am trying to get a list of group members for multiple groups from active directory and export the list into csv file. I am getting names and SamAccountName with my script but I need users Email Addresses as well. If anyone can help me out that would be great. here is my script:

    Import-Module ActiveDirectory
    $groups = Get-Content C:\Groups.txt
    $resultsarray =@()
    foreach ($group in $groups) {
    $resultsarray += Get-ADGroupMember -Id $group | select Name,SamAccountName,@{Expression={$group};Label="Group Name"}
    }

    $resultsarray| Export-csv -path C:\AllADUsers.csv -notypeinformation

  • #104783

    Participant
    Points: 17
    Rank: Member

    Quick and dirty, add this to your "select" section

    @{l="EmailAddress";e={(Get-ADUser $_ -Properties emailaddress).emailaddress}}

    "l" and "e" are just shortened versions of "Label" and "Expression"
    Get-ADUser $_ will get each user in the pipeline as they stream out from from your Get-ADGroupMember command. Then it's just a matter of selecting your desired properties, emailaddress in this instance.

    • #104846

      Participant
      Points: 0
      Rank: Member

      Thank you. Worked as expected. I am also trying to export each group separately into multiple sheets in one excel. Is that practically possible? If so can you please help me.

  • #104981

    Participant
    Points: 0
    Rank: Member

    I am seeking help to export each Active Directory groups and group members separately into multiple sheets in one excel.

    • #104983

      Participant
      Points: 144
      Helping Hand
      Rank: Participant

      You could make your life easier when you use the Powershell module ImportExcel for that.

The topic ‘Export members of multiple groups into single file’ is closed to new replies.