locked
Powershell tip to get desired distribution group name and members in csv format RRS feed

  • Question

  • I'm trying to get a list of every distribution group and the DG Name (column 1) –SEPARATOR- List-Of-All-DG-Member-Email-Addresses-Separated-With-Space (column 2). I'm using "Get-DistributionGroupMember -identity "DistributionListName" | Export-Csv C:\MyFile.Csv", but I have hundreds of DL's and doing them one at a time is extremely tedious.  I'm thinking a get-distributiongroup | get-distributiongroupmember | ft something | export-csv, but I'm not getting it right and could use assistance.

    Saturday, March 3, 2012 3:11 AM

Answers

  • Hi Kanien,

    From your description, I understand that you want use PowerShell to export Groups with Distribution Group Name, members, and members' email address, right?

    I write this script to do that, hope can give you some help:

    $Groups=Get-DistributionGroup
    
    $Groups| foreach{
    
    $GroupName=$_.Name
    
    $Report+=Get-distributionGroupMember -identity $_.identity| select @{Name='Distribution Group'; Expression={[String]::join(";", $GroupName)}}, Name, PrimarySmtpAddress}
    
    $Report| export-csv "c:\MyFile.csv" -notype

    Thanks,
    Evan Liu

    TechNet Subscriber Supportin forum

    If you have any feedback on our support, please contact tngfb@microsoft.com 



    Evan Liu

    TechNet Community Support


    • Edited by Evan LiuModerator Tuesday, March 6, 2012 1:49 AM
    • Marked as answer by Kanien Wednesday, March 7, 2012 1:27 AM
    Monday, March 5, 2012 5:22 AM
    Moderator
  • Hello,

    I didn't see any error in my lab (Exchange 2007), please use this to have a try:

    $Reports=@()
    
    $Groups=Get-DistributionGroup
    
    $Groups| foreach{
    
    $GroupName=$_.Name
    
    $Report=Get-distributionGroupMember -identity $_.identity| select @{Name='Distribution Group'; Expression={[String]::join(";", $GroupName)}}, Name, PrimarySmtpAddress
    
    $Reports=$Reports+$Report
    
    }
    
    $Reports| out-file "c:\MyFile2.txt" 

    Then check whether you will get error or not.

    Thanks,

    Evan Liu

    TechNet Subscriber Supportin forum

    If you have any feedback on our support, please contacttngfb@microsoft.com


    Evan Liu

    TechNet Community Support


    • Edited by Evan LiuModerator Wednesday, March 14, 2012 10:39 AM type
    • Marked as answer by Kanien Sunday, March 18, 2012 10:05 AM
    Wednesday, March 14, 2012 10:38 AM
    Moderator

All replies

  • Take a look at this:

    http://www.energizedtech.com/2010/03/powershell-show-group-membersh.html

    This uses the W2K8 R2 Get-ADGroup functions to enumerate the groups and write each group membership to a separate CSV file.  It does require that you define your own function, but it seems to do the trick.


    Jim McBee - Blog - http://mostlyexchange.blogspot.com

    • Proposed as answer by Jason Sherry Saturday, March 3, 2012 6:40 PM
    Saturday, March 3, 2012 7:33 AM
  • I once wrote a script to list all distribution groups and members of those groups. It uses the Add-Member method, which is slow compared to Select-Object. On the other hand, it allows for a structured approach which is easy to modify and extend.

    See if that helps. (Not Karl Mitschke's script, whom I btw hold in high esteem for his PowerShell skills.)

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


    MCTS: Messaging | MCSE: S+M

    Saturday, March 3, 2012 7:52 AM
  • Saturday, March 3, 2012 8:03 AM
  • Hi Kanien,

    From your description, I understand that you want use PowerShell to export Groups with Distribution Group Name, members, and members' email address, right?

    I write this script to do that, hope can give you some help:

    $Groups=Get-DistributionGroup
    
    $Groups| foreach{
    
    $GroupName=$_.Name
    
    $Report+=Get-distributionGroupMember -identity $_.identity| select @{Name='Distribution Group'; Expression={[String]::join(";", $GroupName)}}, Name, PrimarySmtpAddress}
    
    $Report| export-csv "c:\MyFile.csv" -notype

    Thanks,
    Evan Liu

    TechNet Subscriber Supportin forum

    If you have any feedback on our support, please contact tngfb@microsoft.com 



    Evan Liu

    TechNet Community Support


    • Edited by Evan LiuModerator Tuesday, March 6, 2012 1:49 AM
    • Marked as answer by Kanien Wednesday, March 7, 2012 1:27 AM
    Monday, March 5, 2012 5:22 AM
    Moderator
  • Hello Kanien,

    Any updates on this issue?

    Thanks,

    Evan Liu

    TechNet Subscriber Supportin forum

    If you have any feedback on our support, please contacttngfb@microsoft.com


    Evan Liu

    TechNet Community Support

    Tuesday, March 6, 2012 1:50 AM
    Moderator
  • Hey Evan,

    I'm getting this error for every list but the first one:

    Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.
    At C:\Scripts\distribution_list.ps1:7 char:10
    + $Report+= <<<< Get-distributionGroupMember -identity $_.identity| select @{Name='Distribution Group'; Expression={[St
    ring]::join(";", $GroupName)}}, Name, PrimarySmtpAddress}
        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound

    Wednesday, March 14, 2012 3:57 AM
  • Hello,

    I didn't see any error in my lab (Exchange 2007), please use this to have a try:

    $Reports=@()
    
    $Groups=Get-DistributionGroup
    
    $Groups| foreach{
    
    $GroupName=$_.Name
    
    $Report=Get-distributionGroupMember -identity $_.identity| select @{Name='Distribution Group'; Expression={[String]::join(";", $GroupName)}}, Name, PrimarySmtpAddress
    
    $Reports=$Reports+$Report
    
    }
    
    $Reports| out-file "c:\MyFile2.txt" 

    Then check whether you will get error or not.

    Thanks,

    Evan Liu

    TechNet Subscriber Supportin forum

    If you have any feedback on our support, please contacttngfb@microsoft.com


    Evan Liu

    TechNet Community Support


    • Edited by Evan LiuModerator Wednesday, March 14, 2012 10:39 AM type
    • Marked as answer by Kanien Sunday, March 18, 2012 10:05 AM
    Wednesday, March 14, 2012 10:38 AM
    Moderator
  • Hello,

    Any updates?

    Thanks,

    Evan Liu

    TechNet Subscriber Supportin forum

    If you have any feedback on our support, please contacttngfb@microsoft.com


    Evan Liu

    TechNet Community Support

    Sunday, March 18, 2012 3:52 AM
    Moderator
  • That worked.

    Oh and, thank you :)

    • Edited by Kanien Sunday, March 18, 2012 10:05 AM
    Sunday, March 18, 2012 10:05 AM
  • Even

    I'm getting Unexpected token 'Report' in expression or statement. At line:1 char:44

    What do you thinks?

    Friday, June 1, 2012 12:48 PM
  • Could you show me the exact error?

    Thanks,

    Evan


    Evan Liu

    TechNet Community Support

    Saturday, June 2, 2012 12:49 AM
    Moderator
  • This works great, but the long names are being cut off with three points. Is it possible to not cut them off?
    Tuesday, July 31, 2012 7:40 AM
  • Hi Evan!

    I need the same thing but I must add other information from DistributionGroup in the same script. This attribute is "CustomAttribute1" 

    So it would be:

    DistributionGroupName,CustomAttribute1,Name,PrimarySMTPaddress

    Could you help me, please?


    Daniel S Orlando MCSE - Messaging

    Wednesday, March 12, 2014 7:33 PM