The PowerShell code below will take a CSV list of mailboxes and Users/Groups to remove from that mailbox. This allows you to quickly and efficiently remove old permissions across Exchange.
The CSV is expected to be in the following format –
Mailbox | User |
---|---|
shaun@contoso.com | user@contoso.com |
shared@contoso.com | group@contoso.com |
PowerShell Code
# Import a CSV containing mailbox email and user/group to remove
$CSV = Import-CSV "C:\temp\CalendarPermissionsToRemove.csv"
# Verify that required columns are present in the CSV
$headers = ($CSV | Get-Member -MemberType NoteProperty).Name
if($headers -notcontains 'Mailbox' -or $headers -notcontains 'User') {
throw "Missing required columns in CSV"
}
# Loop through each row assigning output to $result
$result = foreach ($row in $CSV) {
#Create a new PSObject to store data
$usr_reportObject = New-Object PSObject
$usr_reportObject | Add-Member -MemberType NoteProperty -Name "Mail" -Value $row.Mailbox
$usr_reportObject | Add-Member -MemberType NoteProperty -Name "UserToRemove" -Value $row.User
# Remove the specified mailbox permissions, setting up a report as we go
try {
Remove-MailboxFolderPermission -Identity "$($row.Mailbox):\Calendar" -User $row.User -Confirm:$false -ErrorAction Stop -Verbose
$usr_reportObject | Add-Member -MemberType NoteProperty -Name "Outcome" -Value 'Removed'
} catch {
Write-Host -ForegroundColor Red -BackgroundColor Yellow $error[0].Exception.Message
$usr_reportObject | Add-Member -MemberType NoteProperty -Name "Outcome" -Value $error[0].Exception.Message
}
# Output the PS object to the $result array
$usr_reportObject
}
# Display the result
$result