SOLVED

Export all owners email adress for all Office 365 groups

Copper Contributor

Hi!
I have a script that exports all owners e-mail address for each Office 365 group to a csv file. The format of the csv file looks like below, one combination of Group and Owner on each row.

Group 1;email address removed for privacy reasons
Group 1;email address removed for privacy reasons
Group 1;email address removed for privacy reasons
Group 2;email address removed for privacy reasons

The script looks like this: 

$PSScriptRoot
$groups = Get-UnifiedGroup -ResultSize Unlimited | Sort-Object DisplayName
$obj = @()
foreach($group in $groups)
{
foreach($ManagedByDetails in $group.ManagedByDetails)
{
$ret = "" | select UnifiedGroup,ManagedByDetails,Alias
$ret.UnifiedGroup = $group.DisplayName
$ret.Alias = $group.Alias
$ret.ManagedByDetails = $ManagedByDetails | Get-Recipient | select -ExpandProperty PrimarySMTPAddress
$ret
$obj += $ret
}
}
$obj | Export-Csv $PSScriptRoot\Exporter\Office365GroupOwners.csv -NoTypeInformation -Encoding utf8

 

The script is working fine, but with over 6000 groups in the organization it now takes a long time to run, and it often stops. I have done some research on Internet and understand that it would be much faster to run a script against the Graph API. I have tested Tony Redmonds script, https://github.com/12Knocksinna/Office365itpros/blob/master/TeamsGroupsActivityReportV5.PS1, and it works fine but it doesn’t give me the email address of all owners, just one for each group/team. I have tried to see if I could re-use some parts of Tonys script and extract all owners email addresses, but I’m not that skilled in scripting :-(.


So, I’m wondering if someone has a Graph-based script that exports the email address of each owner like my original script? Or any other suggestion to extract the same information.
Thanks!
/Henrik

 

6 Replies

I'm sure we can harass @Tony Redmond to update the script to properly handle multiple owners. Say by removing the zero index out of $GroupData[0]/using a proper loop :)

best response confirmed by Henrik Adolfsson (Copper Contributor)
Solution
My logic for only outputting one contact email address for an owner is that this is what I was asked to do by someone who explicitly requested the addition of this data...

Anyway, replace:

$GroupOwnerEmail = $GroupData[0].Mail }

with

$GroupUserMail = $GroupData.Mail -Join ", "

and you'll get the email addresses of all the owners.

BTW, the script is now at version 5.10, updated today to fix some annoying bugs introduced by Microsoft in the Get-ExoMailboxFolderStatistics cmdlet
I also updated the Microsoft Graph PowerShell SDK version of the group membership report described in https://office365itpros.com/2023/01/19/microsoft-365-groups-report-v2/ to output the email addresses of group owners. That script is probably closer to what you're looking for.
Thanks Tony!
At first it didn't work, but when I changed it to
$GroupOwnerEmail = $GroupData.Mail -Join ", " }
it gave all owners email addresses. I guess that you meant $GroupOwnerEmail and not
$GroupUserMail, or?

It ran very fast and gave me the information that I need, but since all addresses are on the same line, I’m note able to import it to my Power BI report like I normally do. Do you know a way to get the group name/alias and just one owner on each row?

Regards,
Henrik

@Henrik Adolfsson 

 

Yep, $GroupOwnerMail is what you need.

To get the entries on separate lines is more problematic. Here's what I did:

First, create an array of the owner names instead of a string (no join)

 

[array]$GroupOwnerEmail = $GroupData.Mail

 

Then, for the output to the list used to drive the CSV and HTML output, add these lines before the line is generated ( $ReportLine = [PSCustomObject][Ordered]@{ )

 

$OFS="`n`n"
$ContactEmails = $Null
If ($G.GroupContact -ne $Null) { [string]$ContactEmails = $($G.GroupContact) }

 

Finally, make sure that the formatted string with line breaks is output in the report

ContactEmail = $ContactEmails

 

This technique uses the fact that you can define the PowerShell out field separator https://devblogs.microsoft.com/powershell/psmdtagfaq-what-is-ofs/ to include a new line escape sequence. It worked for me but might not be what you need for Power BI. Try it.

Thank you Tony! I will give it a try.

Regards,
Henrik
1 best response

Accepted Solutions
best response confirmed by Henrik Adolfsson (Copper Contributor)
Solution
My logic for only outputting one contact email address for an owner is that this is what I was asked to do by someone who explicitly requested the addition of this data...

Anyway, replace:

$GroupOwnerEmail = $GroupData[0].Mail }

with

$GroupUserMail = $GroupData.Mail -Join ", "

and you'll get the email addresses of all the owners.

BTW, the script is now at version 5.10, updated today to fix some annoying bugs introduced by Microsoft in the Get-ExoMailboxFolderStatistics cmdlet

View solution in original post