List of all distribution lists that users is a member of in Exchange onprem Exchang Online

Roger Roger 4,951 Reputation points
2021-01-27T12:52:11.427+00:00

Hi I have to get a list of all distribution lists that users are members of in Exchange onprem and Exchange online. How can this be done via PowerShell? I have 10 users for which i want to pull it

Microsoft Office Online Server
Microsoft Office Online Server
Microsoft on-premises server product that runs Office Online. Previously known as Office Web Apps Server.
586 questions
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,193 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,357 questions
0 comments No comments
{count} votes

Accepted answer
  1. Eric Yin-MSFT 4,386 Reputation points
    2021-01-28T08:00:48.783+00:00

    Hi,
    Assuming you save the scv file in "c:\temp\name.csv", run the following script in powershell:

       $names= Import-Csv c:\temp\name.csv  
       $Result= @()   
       $groups= Get-DistributionGroup  
       foreach ($name in $names) {  
       foreach($group in $groups){  
       foreach ($member in (Get-DistributionGroupmember $group.name)){  
       if ($member.PrimarySMTPaddress -eq $name.name){  
       $Result1 = New-Object PSObject   
       $Result1 | Add-Member NoteProperty -Name "User" -Value $name.name  
       $Result1 | Add-Member NoteProperty -Name "group" -Value (Get-distributiongroup $group.name).PrimarySMTPaddress  
       $Result += $Result1  
       }}}}  
       $Result|Export-csv -Path c:\temp\result.csv -NoTypeInformation  
    

    It may take some time and the result would be at "c:\temp\result.csv".
    Besides, are there any other user types in your csv like mail contact?


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Vasil Michev 95,666 Reputation points MVP
    2021-01-27T13:22:09.497+00:00
    0 comments No comments

  2. Roger Roger 4,951 Reputation points
    2021-01-28T04:06:40.36+00:00

    if i have users in csv file in the below format

    users
    user1@Company portal .com
    user2@Company portal .com

    can i use the script in this format.
    import.csv c:\input.csv .\DG_MemberOf_inventory.ps1

    0 comments No comments

  3. Roger Roger 4,951 Reputation points
    2021-01-28T10:43:25.787+00:00

    i have tried your syntax of eric it works fine but i dont get any output in exchange onprem, and in exchange online i am getting errors.

    i want to try the technet script of michev. i want to use in the below format but not sure how to use it. in the input.csv i have email addresses in below format
    users
    user1@Company portal .com
    user2@Company portal .com

    $FilePath = "C:\input.csv"
    Import-CSV $FilePath .\script.ps1 | Export-Csv "C:\output.csv" -notypeinformation