Remove user access to multiple shared mailboxes all at once

Exchange 365

Is there a way in powershell to remove a user from all shared mailbox full access lists at once?  We're trying to clean things up and put in place new policies, but some users have 10, 20, 50 attached shared mailboxes and we would prefer not to have to go into each mailbox one at a time to remove them from the full access lists.

Thanks!
sfcandersonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Adam BrownSr Solutions ArchitectCommented:
Yep. If you create a CSV file that has a heading of "username" and put each username on a line, this should clear them all in one shot:

$users = import-csv <csv file path>
$sharedmbxs=get-mailbox | where {_.type -eq "Shared"}
foreach ($user in $users)
{
   foreach ($mbx in $sharedmbxs)
   {
      remove-mailboxpermission $mbx -user $user.username -accessrights fullaccess
   }
}
      

Open in new window


Haven't tested this, so there may be errors should work, though.
Will SzymkowskiSenior Solution ArchitectCommented:
I would do it this way...

Create your csv file
Username    Mailbox
john              Mailbox1
joe                 Mailbox2
mike              Mailbox1
alice               Mailbox3
etc...

$Data = import-csv "c:\filename.csv"
ForEach ($User in $Data) {

     $User.Username
     $User.Mailbox

      Remove-MailboxPermission -Identity $User.Mailbox -User $User.Username -AccessRights FullAccess -InheritanceType All

}

Open in new window


Will.
sfcandersonAuthor Commented:
What I'm more looking for is:

User is  Joe.Schmoe and he has 33 shared mailboxes attached in his Outlook.
Looking for powershell script to remove him from all shared mailboxes at once without having to list each of the mailbox names
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Will SzymkowskiSenior Solution ArchitectCommented:
This should be do-able. I have put something together that should work. See below...
$Mailboxes = Get-mailbox -ResultSize "unlimited"
$User = (read-host "type username to remove")
ForEach ($Mailbox in $Mailboxes) 
    {

        $MailboxesFound = Get-MailboxPermission -User $User | 
                             ? { ($_.AccessRights.ToString() -ne "NT AUTHORITY\SELF") -and ($_.Identity -notlike $User) -or ($_.AccessRights.ToString() -eq "FullAccess") } |
                             Select Identity, User, AccessRights

        $RemovePerms = $MailboxesFound | Export-Csv "c:\tempcsv.csv"

        ForEach ($ID in $RemovePerms) 
            {

                Remove-MailboxPermission -Identity $ID.Identity -User $ID.User -AccessRights $ID.AccessRights -InheritanceType All


      }

               Remove-Item "c:\tempcsv.csv" -force
}

Open in new window



Will.
Will SzymkowskiSenior Solution ArchitectCommented:
I have modified my script above and i have also tested it. The below one now works without any issues.

$User = (read-host "type username to remove")
$Access = "FullAccess"
$Mailboxes = Get-mailbox -ResultSize "unlimited" | Get-MailboxPermission -User $User | 
             ? { ($_.AccessRights.ToString() -ne "NT AUTHORITY\SELF") -and ($_.Identity -notlike $User) -or ($_.AccessRights.ToString() -eq "FullAccess") } |
             Select Identity, User
             

ForEach ($Mailbox in $Mailboxes) 
    {

        $Mailboxes | Export-Csv "c:\tempcsv.csv" -nti
        
        $RemovePerms = Import-csv "c:\tempcsv.csv"

        ForEach ($ID in $RemovePerms) 
            {
                
                $ID.Identity
                $ID.User
                                
                Remove-MailboxPermission -Identity $ID.Identity -User $ID.User -AccessRights $Access -InheritanceType All -confirm:$false


      }

                Remove-Item "c:\tempcsv.csv" -force
                
}

Open in new window


Will.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sfcandersonAuthor Commented:
Thank you so much for your hard work in putting together a script for this.  It has saved us many hours of work!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.