How can I get Owners of a Distribution Group in a CSV, each owner in different row?

Sergio Furgiuele 21 Reputation points
2022-07-08T02:00:49.31+00:00

Hello there!
I am not clearly a Powershell Expert and I need a way to export the owners of a Distribution Group be exported in this way

Group1 - Owner1
Group1 - Owner2
Group1 - Owner3
Group2 - Owner1
Group2 - Owner2

The only thing I found is a way to separate the users in the ManagedBy array separated by ";", but I can´t find a way to fortmat in the way I want. The problem here is that we are talking about a huge amount of groups and the CSV is to use it in another tool, so the format should be in that way.
If someone could give me a hand, would be great

Thanks!

PS
If someone have something that combines the members and owners in the same CSV and same format alltogether, will save my life :)

Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,185 questions
Exchange | Exchange Server | Management
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. KyleXu-MSFT 26,396 Reputation points
    2022-07-11T04:19:34.233+00:00

    @Sergio Furgiuele

    Are you using Exchange online?

    You could save groups that you want checked into a source file:
    219280-1.png

    Then save the script below into a .PS1 file:

    $Data = @()  
    $Groups = import-csv c:/temp/groups.csv  
      
    foreach ($group in $groups){  
        $value = Get-DistributionGroup $group.Name | select DisplayName,@{Expression=" ";Label="ManagedBy";}  
        $temp = (Get-DistributionGroup $group.Name).ManagedBy      
        foreach ($element in $temp){  
            $value.ManagedBy = $element.tostring()  
            $temp2 = $value |select DisplayName,ManagedBy  
            $data+=$temp2  
            }  
    }$data | export-csv c:/temp/Owner.csv -NoTypeInformation  
    

    Run this PS1 file in Exchange online PowerShell. You will could get the output like below:
    219354-3.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.



3 additional answers

Sort by: Most helpful
  1. Aung Zaw Min Thwin 306 Reputation points
    2022-07-08T02:56:36.29+00:00

    Pls try below

    $Groups = @'  
    Name,ManagedBy,DisplayName  
    Group1,"User1;User2;User3","Group 1"  
    Group2,"User1;User2","Group 2"  
    '@ | ConvertFrom-Csv  
      
      
    foreach ($g in $Groups) { foreach ($m in $g.managedBy.split(';')) { $g | select Name, DisplayName, @{n='Manager'; e= {$m}} }  }  
      
    

  2. Newbie Jones 1,386 Reputation points
    2022-07-12T15:41:29.167+00:00

    How about the following?

    $groups = Import-CSV import-filename.csv  
      
    foreach ($group in $groups){  
      ForEach ($account in (Get-DistributionGroup $group.Name).ManagedBy) {  
          $props = [ordered]@{                     
          Group = $group   
          ManagedBy = $account  
          DisplayName = (Get-ADUser $account -properties DisplayName).DisplayName}  
          [PSCustomObject]$props  
      }  
    } Export-CSV export-filename.csv -NoTypeInformation  
    
    0 comments No comments

  3. Alessandro Brambilla 0 Reputation points
    2024-02-07T10:57:34.1866667+00:00

    Why always doing useless and sometimes complex script with variables when you can use a SINGLE LINE command?
    I freaking hate scripts when not necessary.

    Get-DistributionGroup | select name,managedby | Export-csv "c:\temp\ExportDLs.csv" -NoTypeInformation

    The end. With that you'll have the file you need (I also needed this and searched to obtain a single line easy command).


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.