locked
get distribution list and members of the list RRS feed

  • Question

  • question to all, i am trying to retrieve all our distribution list and members of each...

    the script that i ran was:

    Get-DistributionGroup|Get-DistributionGroupMember

    this displays all distribution group and members in a list. i was trying to get a display that shows all members under each distribution group. is this possible?

    Thursday, October 13, 2011 12:29 AM

Answers

  • This was tested with Exchange 2007 and PowerShell v.1. You can easily add a Write-Host $_ before the For loop and Write-Host $member inside the For loop to get screen output or just see what is going on.

    # Initialize array with two fields:

    # Distribution group, Members

    $totalObj = @()

    # Retrieve all DGs

    $temp = Get-DistributionGroup -ResultSize Unlimited |

    # Loop through all distribution groups

    ForEach-Object {

    # Add the members of the DG to an array

    [array]$mem = Get-DistributionGroupMember -id $_

    # Loop through the DG and assign each member name to the variable $member

    for ($i = 0; $i -lt $mem.Count; $i++) {

    $member = $mem[$i].name

    # Create instance of object of type .NET

    $obj = New-Object System.Object

    # Add the name of the DG to the object

    $obj | Add-Member -MemberType NoteProperty -Value $_.Name -Name 'Distribution Group' -Force

    # Add the member name to the object

    $obj | Add-Member -MemberType NoteProperty -Value $member -Name 'Members' -Force -PassThru

    # Add the object to the array

    $totalObj += $obj

    }

    }

    # Pipe output to .csv file

    $totalObj | Export-Csv -Encoding 'Unicode' c:\temp\ngtest.csv

    The output is written like this to the csv. file:

    "Distribution Group",Members

    "All Users","Jon-Alfred Smith"

    "All Users","Julie Smith"

    "Windows SBS Administrators","Standard User with administration links"

    "Windows SBS Administrators","Jon-Alfred Smith"

    If you just want to have the name of the DG once, change this line: Only add the name the first time, when the counter is zero:

     

    # Add the name of the DG to the object

    if ($i -eq 0) {

    $obj | Add-Member -MemberType NoteProperty -Value $_.Name -Name 'Distribution Group' -Force

    }



    MCTS: Messaging | MCSE: S+M
    • Marked as answer by newguy0419 Friday, October 14, 2011 12:20 AM
    Thursday, October 13, 2011 7:32 PM
  • Something is wrong with what the web pages accept right now. The formating is better here:

    Powershell command to list all distribution groups and members of those groups
    http://social.technet.microsoft.com/Forums/en-US/exchangesvradmin/thread/63ad55d1-1393-4533-8065-1d7cbd8657fa/


    MCTS: Messaging | MCSE: S+M
    • Marked as answer by newguy0419 Friday, October 14, 2011 12:20 AM
    Thursday, October 13, 2011 7:36 PM

All replies

  • $DG=Get-DistributionGroup

    foreach ($EachDG in $DG)

    {

    Write-host "Group name: $($EachDG.Displayname) "

    Get-DistributionGroupMember -Identity $EachDG

    }

    Didn't tested it so there mighht be syntax error


    Jasjit Singh Dhindsa | ITIL v3 | IASA Foundation Certified | MCITP:Exchange 2010 | Exchange 2007 | MCTS:OCS 2007 | Exchange 2010 | Exchange 2007 | MCSA:Messaging | Security | MCSE:Messaging | Security | MCP
    Thursday, October 13, 2011 4:37 PM
  • This was tested with Exchange 2007 and PowerShell v.1. You can easily add a Write-Host $_ before the For loop and Write-Host $member inside the For loop to get screen output or just see what is going on.

    # Initialize array with two fields:

    # Distribution group, Members

    $totalObj = @()

    # Retrieve all DGs

    $temp = Get-DistributionGroup -ResultSize Unlimited |

    # Loop through all distribution groups

    ForEach-Object {

    # Add the members of the DG to an array

    [array]$mem = Get-DistributionGroupMember -id $_

    # Loop through the DG and assign each member name to the variable $member

    for ($i = 0; $i -lt $mem.Count; $i++) {

    $member = $mem[$i].name

    # Create instance of object of type .NET

    $obj = New-Object System.Object

    # Add the name of the DG to the object

    $obj | Add-Member -MemberType NoteProperty -Value $_.Name -Name 'Distribution Group' -Force

    # Add the member name to the object

    $obj | Add-Member -MemberType NoteProperty -Value $member -Name 'Members' -Force -PassThru

    # Add the object to the array

    $totalObj += $obj

    }

    }

    # Pipe output to .csv file

    $totalObj | Export-Csv -Encoding 'Unicode' c:\temp\ngtest.csv

    The output is written like this to the csv. file:

    "Distribution Group",Members

    "All Users","Jon-Alfred Smith"

    "All Users","Julie Smith"

    "Windows SBS Administrators","Standard User with administration links"

    "Windows SBS Administrators","Jon-Alfred Smith"

    If you just want to have the name of the DG once, change this line: Only add the name the first time, when the counter is zero:

     

    # Add the name of the DG to the object

    if ($i -eq 0) {

    $obj | Add-Member -MemberType NoteProperty -Value $_.Name -Name 'Distribution Group' -Force

    }



    MCTS: Messaging | MCSE: S+M
    • Marked as answer by newguy0419 Friday, October 14, 2011 12:20 AM
    Thursday, October 13, 2011 7:32 PM
  • Something is wrong with what the web pages accept right now. The formating is better here:

    Powershell command to list all distribution groups and members of those groups
    http://social.technet.microsoft.com/Forums/en-US/exchangesvradmin/thread/63ad55d1-1393-4533-8065-1d7cbd8657fa/


    MCTS: Messaging | MCSE: S+M
    • Marked as answer by newguy0419 Friday, October 14, 2011 12:20 AM
    Thursday, October 13, 2011 7:36 PM
  • thanks....helped alot
    Friday, October 14, 2011 12:20 AM