This script removes a users access to all User Mailboxes within Exchange
Param( #Defines Parameters that will be used within this script.
[Parameter(Mandatory=$true)] #This is an attribute applied to the parameter. It specifies that a parameter is required.
[string]$userToRemoveAccess #[String] is the type of parameter. $userToRemoveAccess is the name of the parameter.
)
Connect-ExchangeOnline
# This grabs all USER mailboxes and not SHARED
$mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox | Where-Object { $_.RecipientTypeDetails -ne "SharedMailbox" }
# This iterates through each mailbox using a FOR Loop and removes '$userToRemoveAccess' full access from the user mailbox ($mailboxEmail)
foreach ($mailbox in $mailboxes) {
$mailboxEmail = $mailbox.UserPrincipalName
Remove-MailboxPermission -Identity $mailboxEmail -User $userToRemoveAccess -AccessRights FullAccess -Confirm:$false
# Prints a list of the current delegation of user's mailbox. This is so you can check delegation as the script works through the list of user mailboxes
Get-MailboxPermission -Identity $mailboxEmail | Where-Object { $_.AccessRights -like "FullAccess" }
}
# Disconnect from Exchange Online
Disconnect-ExchangeOnline