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?
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.
0
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.
0
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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.
0
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.
1

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!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.