Connect to Office 365 with PowerShell
Specific:
$Username = Read-Host "Enter the username to check mailbox permissions for"
Write-Host "Fetching all mailboxes..." -ForegroundColor Cyan
$mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox,SharedMailbox
$results = [System.Collections.Generic.List[object]]::new()
$i = 0
$total = $mailboxes.Count
foreach ($mailbox in $mailboxes) {
$i++
Write-Progress -Activity "Checking mailbox permissions" `
-Status "$i of $total - $($mailbox.PrimarySmtpAddress)" `
-PercentComplete (($i / $total) * 100)
$permissions = Get-MailboxPermission -Identity $mailbox.ExchangeGuid.ToString() -ErrorAction SilentlyContinue |
Where-Object {
-not $_.IsInherited -and
$_.AccessRights -ne $null -and
($_.User -like "*$Username*" -or $_.User -eq $Username)
}
foreach ($perm in $permissions) {
$results.Add([PSCustomObject]@{
DisplayName = $mailbox.DisplayName
PrimarySmtpAddress = $mailbox.PrimarySmtpAddress
MailboxType = $mailbox.RecipientTypeDetails
GrantedTo = $perm.User
AccessRights = ($perm.AccessRights -join ', ')
Deny = $perm.Deny
})
}
}
Write-Progress -Activity "Checking mailbox permissions" -Completed
if ($results.Count -gt 0) {
Write-Host "`n$($results.Count) mailbox(es) accessible by ${Username}:" -ForegroundColor Green
$results | Sort-Object MailboxType, DisplayName | Format-Table -AutoSize
} else {
Write-Host "`nNo mailbox permissions found for ${Username}" -ForegroundColor Yellow
}
Get-Mailbox | Get-MailboxPermission -User Username
All:
Get-Mailbox -ResultSize Unlimited | ForEach-Object {Get-MailboxPermission -Identity $_.DistinguishedName | Select-Object Identity,User,AccessRights | Where-Object {($_.user -like '*@*')}} | Export-Csv C:\Permissions.csv -NoTypeInformation
Calendar:
$userToCheck = Read-Host -Prompt "Enter the username to search for (partial name is ok)"
Get-Mailbox | ForEach-Object {
$email = $_.PrimarySmtpAddress
$displayName = $_.DisplayName
Get-MailboxFolderPermission -Identity ($email + ":\Agenda") -ErrorAction SilentlyContinue | Select-Object @{Name='Owner';Expression={$displayName}}, User, AccessRights, FolderName
Get-MailboxFolderPermission -Identity ($email + ":\Calendar") -ErrorAction SilentlyContinue | Select-Object @{Name='Owner';Expression={$displayName}}, User, AccessRights, FolderName
} | Where-Object {$_.User -like "$userToCheck*"}