Exchange – Remove List of Permissions from Exchange Mailboxes

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 –

MailboxUser
shaun@contoso.comuser@contoso.com
shared@contoso.comgroup@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
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.