locked
Group Membership report script - Modify to change format of output RRS feed

  • Question

  • Hi all,

    Slowly getting to grips with Powershell. Such a powerful tool.

    I've been tasked to develop a reporting script that will output a list of members of a set of groups and have found one of the scripts here to be a great starting point.

    So this is the script:

    "Import-Module ActiveDirectory
    cd AD:

    $MemberList = New-Item -Type file -Force “C:\Scripts\GroupMembers.csv”

    Import-Csv “C:\scripts\grps.csv” | ForEach-Object {
     $GName = $_.Samaccountname
     $group = Get-ADGroup $GName
     $group.Name | Out-File $MemberList -Encoding Unicode -Append
      foreach ($member in Get-ADGroupMember $group) {$member.SamaccountName | Out-File $MemberList -Encoding Unicode -Append}
    $nl = [Environment]::NewLine | Out-File $MemberList -Encoding ASCII -Append
    }

    "

    The output lists the group name and the members of said group using Sam account name.

    Ideally I would like that the email address of the user be in the output instead of SAMACCOUNTNAME however I realise that this may not easily be achievable due to the output of get-adgroupmember.

    I'd also like if the output contained each of the email addresseses of the group members to be on the same line seperated by a semicolon in one cell directly under the group name.

    EG:

    GROUPNAME1

    user1@mail.com;user1@mail.com;user4@mail.com

    GROUPNAME2

    user15:mail.com

    Etc

    Can either of these requirements be achieved from modifying the script above and any help would be much appreciated.

    Thanks for any assistance.

    Thursday, October 30, 2014 1:11 PM

Answers

  • Hi,

    Try it this way:

    Import-Csv .\grps.csv | ForEach {
    
        $groupName = $_.SamAccountName
    
        $groupMembers = Get-ADGroupMember -Identity $groupName -Recursive | ForEach {
    
            Get-ADUser -Identity $_.SamAccountName -Properties EmailAddress | 
                Select -ExpandProperty EmailAddress
    
            }
    
        $props = @{
            GroupName = $groupName
            Members = ($groupMembers | Sort) -join ';'
        }
    
        New-Object PsObject -Property $props
    
    } | Select GroupName,Members | Export-Csv .\groupMemberships.csv -NoTypeInformation


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    • Marked as answer by jpkilbane Thursday, October 30, 2014 2:03 PM
    Thursday, October 30, 2014 1:48 PM

All replies

  • check this one and I think u can use property user.email

    http://techtrainingnotes.blogspot.com/2010/12/sharepoint-powershell-script-to-list.html


    Kind Regards, John Naguib Technical Consultant/Architect MCITP, MCPD, MCTS, MCT, TOGAF 9 Foundation

    Thursday, October 30, 2014 1:15 PM
  • Hi,

    Try it this way:

    Import-Csv .\grps.csv | ForEach {
    
        $groupName = $_.SamAccountName
    
        $groupMembers = Get-ADGroupMember -Identity $groupName -Recursive | ForEach {
    
            Get-ADUser -Identity $_.SamAccountName -Properties EmailAddress | 
                Select -ExpandProperty EmailAddress
    
            }
    
        $props = @{
            GroupName = $groupName
            Members = ($groupMembers | Sort) -join ';'
        }
    
        New-Object PsObject -Property $props
    
    } | Select GroupName,Members | Export-Csv .\groupMemberships.csv -NoTypeInformation


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    • Marked as answer by jpkilbane Thursday, October 30, 2014 2:03 PM
    Thursday, October 30, 2014 1:48 PM
  • Guys,

    Much appreciated.

    Mike, the script you provided did exactly as I wanted. Much Much appreciated.

    I'll spend some time trying to figure out how it does that as there is so much learning there!

    Thanks again to both of you,

    Thursday, October 30, 2014 2:04 PM
  • You're welcome, glad I could help out.

    Here's the same code with some comments and syntax links:

    # Import the CSV file and process each record
    Import-Csv .\grps.csv | ForEach {
    
        # Set the group name as a variable for easy use later
        # This really isn't necessary, but I do it for easy reading
        $groupName = $_.SamAccountName
    
        # Get the group members of the current group and process each user
        # The output of this loop will be stored in a single variable
        $groupMembers = Get-ADGroupMember -Identity $groupName -Recursive | ForEach {
    
            # Get the user account and select only the EmailAddress property
            Get-ADUser -Identity $_.SamAccountName -Properties EmailAddress | 
                Select -ExpandProperty EmailAddress
    
        }
        
        # Create a hashtable from the data above
        # The group members are sorted and then joined using ; as a delimiter
        $props = @{
            GroupName = $groupName
            Members = ($groupMembers | Sort) -join ';'
        }
    
        # Create a new object based on the hashtable above
        New-Object PsObject -Property $props
    
    } | Select GroupName,Members | Export-Csv .\groupMemberships.csv -NoTypeInformation
    # Select is used to ensure the correct column order and then an output CSV is created

    http://ss64.com/ps/import-csv.html

    http://ss64.com/ps/foreach-object.html

    http://ss64.com/ps/get-adgroupmember.html

    http://ss64.com/ps/get-aduser.html

    http://ss64.com/ps/select-object.html

    http://ss64.com/ps/syntax-hash-tables.html

    http://ss64.com/ps/new-object.html

    http://ss64.com/ps/export-csv.html


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Friday, October 31, 2014 2:54 PM