Wednesday, July 18, 2012

Add / Remove Site Collection Administrator to All Site Collections

One fine morning, your boss could walk up to you and say that he wants a particular user to be added to all the site collections in a web application or could go one step further and say that this user needs to be added as a site collection administrator to all the site collections across all the web applications in a farm. You might think that....it shouldn't be all that difficult. There are just a handful of site collections in my web application. But well my situation was that there were not a handful but a few hundred handful of those. In fact about 700 of them in just one web application.

Now here are your options...

  1. Tell your boss "Not in a million years...." and it'd be time to pick the resume again.
  2. You can go to every site collection's settings page and add the user and this would probably take a few weeks to do.
  3. You can have the user request access to all the site collections and then have it approved. But this would only work, if the option to request access is enabled.
  4. One another geeky option is to say that you'd write a small .NET application that makes use of SharePoint's web services or the object model to do the job.
  5. Or you can get even more smarter and say that you'd do that using PowerShell in just a minute and sound like the smartest guy in the room.
And here is how you do it. The first command adds the user as a site collection administrator to all the site collections in the farm in one go. And the second one does it for a specific web application.
 Get-SPSite -Limit All | %{New-SPUser -UserAlias "email@domain.anything" -SiteCollectionAdmin:$true -PermissionLevel "Full Control" -Web $_.Url}  
 Get-SPSite -Limit All –WebApplication http://mywebapplication.mydomain.com | %{New-SPUser -UserAlias "email@domain.anything" -SiteCollectionAdmin:$true -PermissionLevel "Full Control" -Web $_.Url}  

You can use the same logic of command chaining presented here with the Remove-SPUser cmdlet in PowerShell to remove a user from multiple sites in one go. But be careful with what you're doing, because yo u don't want to listen to the music later if something went south.

2 comments:

  1. Remove-SPUser command is not working,can you please post the power shell script for removing a user from site collection administrators.

    ReplyDelete