Entra / Microsoft 365 · Groups
Report d ls and managers
Quick and dirty script to report the managers of distribution lists.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-ExchangeOnline -ShowBanner:$false
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
CLS# Check that we are connected to Exchange Online$ModulesLoaded = Get-Module | Select NameIf (!($ModulesLoaded -match "ExchangeOnlineManagement")) {Write-Host "Please connect to the Exchange Online Management module and then restart the script"; break}$OrgName = (Get-OrganizationConfig).Name$CreationDate = Get-Date -format g$Version = "1.0"$ReportFile = "c:\temp\DLManagersReport.html"$CSVFileMembers = "c:\temp\DLManagersReport.csv"Write-Host "Finding Distribution lists in" $OrgName "..."[array]$DLs = Get-DistributionGroup -ResultSize Unlimited -Filter {RecipientTypeDetails -ne "Roomlist"} | Select DisplayName, ExternalDirectoryObjectId, ManagedByIf (!($DLs)) { Write-Host "No distribution lists found - exiting" ; break }$Report = [System.Collections.Generic.List[Object]]::new()Write-Host "Reporting Distribution lists and managers..."ForEach ($DL in $DLs) {$ManagerList = [System.Collections.Generic.List[Object]]::new()ForEach ($Manager in $DL.ManagedBy) {$Recipient = Get-Recipient -Identity $Manager -ErrorAction SilentlyContinueIf (!($Recipient)) { # Can't resolve manager$Recipient = "Unknown user" }$ManagerLine = [PSCustomObject][Ordered]@{DisplayName = $Recipient.DisplayNameUPN = $Recipient.WIndowsLiveID }$ManagerList.Add($ManagerLine)} # End processing managers$Managers = $ManagerList.DisplayName -join ", "$DLLine = [PSCustomObject][Ordered]@{DisplayName = $DL.DisplayNameManagers = $ManagersEMailAddress = $DL.PrimarySmtpAddress }$Report.Add($DLLine)} # End processing DL# Create the HTML report$htmlhead="<html><style>BODY{font-family: Arial; font-size: 8pt;}H1{font-size: 22px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}H2{font-size: 18px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}H3{font-size: 16px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}TABLE{border: 1px solid black; border-collapse: collapse; font-size: 8pt;}TH{border: 1px solid #969595; background: #dddddd; padding: 5px; color: #000000;}TD{border: 1px solid #969595; padding: 5px; }td.pass{background: #B7EB83;}td.warn{background: #FFF275;}td.fail{background: #FF2626; color: #ffffff;}td.info{background: #85D4FF;}</style><body><div align=center><p><h1>Distribution List Manager Report</h1></p><p><h2><b>For the " + $Orgname + " organization</b></h2></p><p><h3>Generated: " + (Get-Date -format g) + "</h3></p></div>"$htmlbody1 = $Report | ConvertTo-Html -Fragment$htmltail = "<p>Report created for: " + $OrgName + "</p>" +"<p>Created: " + $CreationDate + "<p>" +"<p>-----------------------------------------------------------------------------------------------------------------------------</p>"+"<p>Number of distribution lists found: " + $DLs.Count + "</p>" +"<p>-----------------------------------------------------------------------------------------------------------------------------</p>"+"<p>Distribution List Manager Report<b> " + $Version + "</b>"$htmlreport = $htmlhead + $htmlbody1 + $htmltail$htmlreport | Out-File $ReportFile -Encoding UTF8$Report | Export-CSV -NoTypeInformation $CSVFileMembersCLSWrite-Host "All done. Output files are" $CSVFileMembers "and" $ReportFile
Attribution
Author
Office365itpros