Hoppa till innehållet
Deal With IT

PowerShell: Exports all shared mailboxes with permissions in Microsoft 365

A script that connects to Microsoft 365, retrieves every shared mailbox, and lists the users who have permissions on each one — exported to a CSV file.

Pontus 1 min läsning

artikelbild: terminalfönster — 16:9

This script will connect to Microsoft 365, retrieve all shared mailboxes, and list the users who have permissions on each mailbox. The results will be exported to a CSV file.

The script

# Install ExchangeOnlineManagement module if not already installed
if (-not (Get-Module -ListAvailable -Name ExchangeOnlineManagement)) {
    Install-Module ExchangeOnlineManagement -Force -Scope CurrentUser
}

# Import the module
Import-Module ExchangeOnlineManagement

# Connect to Exchange Online (prompt for credentials)
Connect-ExchangeOnline -UserPrincipalName admin@yourdomain.com

# Output file path
$outputFile = "C:\temp\SharedMailboxPermissions.csv"

# Retrieve all shared mailboxes
$sharedMailboxes = Get-Mailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited

# Initialize an array to store the results
$results = @()

# Loop through each shared mailbox
foreach ($mailbox in $sharedMailboxes) {
    $permissions = Get-MailboxPermission -Identity $mailbox.PrimarySmtpAddress | Where-Object {
        $_.User -notlike "NT AUTHORITY\*" -and $_.User -notlike "S-1-5-*" # Exclude system accounts
    }

    foreach ($perm in $permissions) {
        $results += [PSCustomObject]@{
            Mailbox     = $mailbox.PrimarySmtpAddress
            User        = $perm.User
            AccessRights = $perm.AccessRights -join ", "
        }
    }
}

# Export results to CSV
$results | Export-Csv -Path $outputFile -NoTypeInformation

# Disconnect from Exchange Online
Disconnect-ExchangeOnline -Confirm:$false

Write-Host "Shared mailbox permissions exported to $outputFile"

Explanation

  • It retrieves all shared mailboxes using Get-Mailbox -RecipientTypeDetails SharedMailbox.
  • It fetches permissions using Get-MailboxPermission and filters out system accounts.
  • The output is stored in C:\temp\SharedMailboxPermissions.csv.

Permissions explained

  • FullAccess — User can open and manage mailbox items.
  • SendAs — User can send emails as the shared mailbox.
  • SendOnBehalf — User can send emails on behalf of the shared mailbox.

Prerequisites

  • You must be a Microsoft 365 admin or have Exchange admin privileges.
  • Replace admin@yourdomain.com with your admin email.
Dela artikeln LinkedIn

Har ni ett problem vi borde skriva om?

Hälften av artiklarna här började som en kundfråga. Ställ er fråga — svaret kanske blir nästa guide.