SharePoint Online: Set Admin for many sites using PowerShell

 

This simple script lets you set an admin for multiple sites listed in a CSV file. It is helpful for when you want to perform admin actions on multiple sites 

$SPAdminURL = "https://contoso-admin.sharepoint.com"
Connect-SPOService -Url $SPAdminURL
#$siteInfo = Get-SPOSite -Limit ALL |Select GroupId, URL
$siteInfo = Import-CSV C:\SWSetup\SharepointSites.csv
$AcctName = "username@contoso.com"
 
foreach ($s in $siteInfo)
{
  Set-SPOUser -Site $s.URL -IsSiteCollectionAdmin $true -LoginName $AcctName


When you are finished, re-run the script, changing the $true to $false in Set-SPOUser.

If you have a O365 group to set as the owner, it's a little more complicated. Here is the script to do that:


$SPAdminURL = "https://contoso-admin.sharepoint.com"
Connect-SPOService -Url $SPAdminURL
Connect-PnPOnline -Url $SPAdminURL -Interactive
$siteInfo = Get-SPOSite -Limit ALL |Select GroupId, URL
#$siteInfo = Import-CSV C:\SWSetup\SharepointSites.csv
 
#Multiple Sites with Group Owner
Function EnsureLoginName($userOrGroupName) {
    $web = Get-PnPWeb
    $userOrGroup = $web.EnsureUser($userOrGroupName)
    $web.Context.Load($userOrGroup)
    $web.Context.ExecuteQuery()
    Return $userOrGroup.LoginName
}

$GroupName = EnsureLoginName "SharePoint Admins"
foreach ($s in $siteInfo)
{    Write-Host $s.Url
     Set-SPOUser -Site $s.URL -IsSiteCollectionAdmin $true -LoginName $GroupName   
}


Sources:

Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.