Add a Site Collection Administrator to all sites in a Web Application


For anybody who reads my posts or even glances at the titles of said posts, they would know by now that I’m quite the PowerShell geek. It’s my favorite administration and automation tool in the SharePoint realm.

This morning I had a need to add a user as a Site Collection Administrator to all sites in a web application. Sort of an unusual request, sure – and yes, I could’ve granted Full Control via the Web Application User Policy. I didn’t want to do that.

I set out on what was a very quick adventure, creating a PowerShell Function to perform my task automatically.

Here’s an explanation of my approach and result, the actual script is linked and downloadable:

Use Get-SPWebApplication and Get-SPSite to retrieve all site collections in a web application. The Web App Url is provided via a parameter to the function, the site collections are stored in a variable called AllSites. The AllSites variable is then run through a foreach loop which uses Set-SPSite to set the SecondaryOwnerAlias property of the SPSite object.

Note: I used my Set-SPSiteLock function as a starting point, here’s that post if you missed it.

Here’s the PowerShell Function:

function Add-SiteCollectionAdministrator {
<#
.SYNOPSIS
Use this PowerShell script to add an account as Site Collection Administrator to all sites in a Web Application.

.DESCRIPTION
This PowerShell script uses Set-SPSite to add an account as Site Collection Administrator to an entire SharePoint Web Application.

.EXAMPLE
Add-SiteCollectionAdministrator -WebAppUrl http://intranet -Account "DOMAIN\Administrator"

This example adds the account DOMAIN\Administrator as a Site Collection Administrator to each site collection in the http://intranet web application.

.NOTES
Use this PowerShell script to add an account as Site Collection Administrator to all sites in a Web Application.

.LINK
http://www.iccblogs.com/blogs/rdennis
 http://twitter.com/SharePointRyan
 
.INPUTS
None

.OUTPUTS
None
#>

## Input Parameters ##
[CmdletBinding()]
Param(
[string]$WebAppUrl=(Read-Host "Please enter a Web Application URL (Example: http://intranet)"),
[string]$Account=(Read-Host "Please enter an account (Example: DOMAIN\Administrator)")
)
## Add SharePoint Snap-in ##
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
### No need to modify these ###
$WebApp = Get-SPWebApplication $WebAppUrl
$AllSites = $WebApp | Get-SPSite -Limit ALL

############################# The Script - no need to modify this section #############################
## Start SP Assignment for proper disposal of variables and objects ##
Write-Host "Starting SP Assignment..." -ForegroundColor Green
Start-SPAssignment -Global
## Use a ForEach-Object loop and Set-SPSiteAdministration to set an entire web application ##
Write-Host "Adding $Account to $WebAppUrl as a Site Collection Administrator..." -ForegroundColor Yellow
$AllSites | ForEach-Object { Set-SPSite -SecondaryOwnerAlias $Account -Identity $_.url }
## End SP Assignment for proper disposal of variables and objects ##
Write-Host "Disposing of SP Objects.." -ForegroundColor Green
Stop-SPAssignment -Global
## Tell us it's done ##
Write-Host "Finished!" -ForegroundColor Green
}
Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s