Its a simple straight forward PowerShell script for adding a new user as site Collection administrator to all available SharePoint Online sites
It is easy to add a Site collection administrator to a single site from portal itself, but in my case I have more than 100+ sites in our tenant and there is a new resource joined in our team to who needs access to start his work. So PowerShell script makes life easier.
$AdminSiteURL = "https://cogsharepoint-admin.sharepoint.com/"
$AdminName = "SPAdmin@cogsharepoint.onmicrosoftcom"
$NewUserName = "NewUser@cogsharepoint.onmicrosoftcom"
#Admin User Name Password to connect
$Password = Read-host -assecurestring "Enter Password for $AdminName"
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Password
#Connect to SharePoint Online using Admin credentials
Connect-SPOService -url $AdminSiteURL -credential $Credential
$Sites = Get-SPOSite -Limit ALL
Foreach ($Site in $Sites)
Write-host "Adding Site Collection Admin for:"$Site.URL
Set-SPOUser -site $Site -LoginName $NewUserName -IsSiteCollectionAdmin $True