try { # Import admin credentials $serverAddress = Get-Content -Path ".\DefaultServer.txt" $credential = Import-Clixml -Path .\AdminCredential.xml # Establish session with Exchange Server $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$serverAddress/PowerShell/ -Authentication Kerberos -Credential $credential Import-PSSession $session -DisableNameChecking # Define output file path in the user's Downloads folder $outputFile = "$env:USERPROFILE\Downloads\SharedMailboxesReport.txt" # Create a report header "Shared Mailboxes Report" | Out-File $outputFile # Retrieve all databases $databases = Get-MailboxDatabase -Status | Select-Object Name foreach ($db in $databases) { # Retrieve mailboxes under the database and filter for shared mailboxes $sharedMailboxes = Get-Mailbox -Database $db.Name | Where-Object {$_.RecipientTypeDetails -eq "SharedMailbox"} | Sort-Object DisplayName # Write shared mailboxes to the file foreach ($mb in $sharedMailboxes) { # Get mailbox size using the unique DistinguishedName $mbStats = Get-MailboxStatistics -Identity $mb.DistinguishedName | Select-Object TotalItemSize # Write data to file without formatting $output = "$($mb.DisplayName), $($mb.PrimarySmtpAddress), $($mbStats.TotalItemSize)" $output | Out-File $outputFile -Append } } Write-Host "Report saved to $outputFile" -ForegroundColor Green Remove-PSSession $session } catch { Write-Host "Failed to retrieve shared mailbox details. Error" -ForegroundColor Red Write-Host $_ }