Adding user/ad group to all sitecollections as sitecollection administrator

A typical task in reorganizing an already existing SharePoint farm is to add a specific user or AD group as a site collection administrator.

The best way to do this is by leveraging PowerShell.

    Add user as site collection administrator to 
    all site collections of web application 
    October 2021 

# Make way for the script 

# Get parameter for reusability of this script 
    [string]$webapplicationUrl = ""

# Load SharePoint PowerShell thingies 
Add-PSSnapin -Name "microsoft.sharepoint.powershell" 

# Check if $username is not empty 
if ($username -eq $null) {
    Write-Host "Variable 'username' must not be empty. "

# Check input variable $webapplicationUrl 
Write-Host "Check if web application is valid... " -NoNewline 
$webapplication = Get-SPWebApplication -Identity $webapplicationUrl 

if ($webapplication -eq $null) {
    Write-Host "Error. Webapplication with URL '$($webapplicationUrl)' does not exist. " -ForegroundColor Red
else {
    Write-Host "Done. " -ForegroundColor Green

# Iterate through all site collections and assign user as site collection admin 
foreach ($sitecollection in $webapplication.Sites) {
    $adminuser = $sitecollection.RootWeb.EnsureUser($username) 

    # Add user if not already a site collection admin 
    if ($adminuser.IsSiteAdmin -eq $true) {
        Write-Host "User '$($adminuser)' is already site collection admin in '$($sitecollection.Url)'. "         
    else {
        $adminuser.IsSiteAdmin = $true 
        Write-Host "User '$($adminuser)' is now site collection admin on '$($sitecollection.Url)'. "

So long…