Add a user or group to site collection administrators group of every site collection - via powershell - Fabian Neve

About SharePoint, Javascript, PowerShell, and other technical stuff

Tuesday, January 16, 2018

Add a user or group to site collection administrators group of every site collection - via powershell

Use following scripts:

This script adds a user or group to the Site Collection Administrators Group of every Site Collection of a Web Application. Define your parameters in line 1 and line 2.

$AccountList = @("Domain\UserOrGroup")
$webapp = Get-SPWebApplication -Identity https://webapplicationurl/
foreach ($SiteCollection in $webapp.Sites)
{
write-host $SiteCollection.url
$spweb = Get-SPWeb $SiteCollection.url
foreach ($Account in $AccountList)
{
$user = Get-SPUSER -identity $Account -web $SiteCollection.url -ErrorAction SilentlyContinue
if ($user -eq $null)
{
$SPWeb.ALLUsers.ADD($Account, "", "", "Added by SiteCollectionAdminScript")
$user = Get-SPUSER -identity $Account -web $SiteCollection.url
Write-host "Added user $Account to URL $SPWeb.URL" -Foregroundcolor green
}
else
{
Write-host "user $Account was already in URL " $SPWeb.URL -Foregroundcolor red
}
if ($user.IsSiteAdmin -ne $true)
{
$user.IsSiteAdmin = $true
$user.Update()
Write-host "$account has been made an admin on $SPWeb.URL" -Foregroundcolor green
}
else
{
Write-host "$account was already an admin on $SPWeb.URL" -Foregroundcolor red
}
}
}


or

This script adds a user or group to the Site Collection Administrators Group of every Site Collection Farm-wide. Define your User or Group in line 2.

#example: $AccountList = @("DOMAIN\User" , "DOMAIN\Group")
$AccountList = @("DOMAIN\User")
$wapps = Get-SPWebApplication
Foreach($webapp in $wapps)
{
foreach ($SiteCollection in $webapp.Sites)
{
write-host $SiteCollection.url
$spweb = Get-SPWeb $SiteCollection.url
foreach ($Account in $AccountList)
{
$user = Get-SPUSER -identity $Account -web $SiteCollection.url -ErrorAction SilentlyContinue
if ($user -eq $null)
{
$SPWeb.ALLUsers.ADD($Account, "", "", "Added by SiteCollectionAdminScript")
$user = Get-SPUSER -identity $Account -web $SiteCollection.url
Write-host "Added user $Account to URL $SPWeb.URL" -Foregroundcolor green
}
else
{
Write-host "user $Account was already in URL " $SPWeb.URL -Foregroundcolor red
}
if ($user.IsSiteAdmin -ne $true)
{
$user.IsSiteAdmin = $true
$user.Update()
Write-host "$account has been made an admin on $SPWeb.URL" -Foregroundcolor green
}
else
{
Write-host "$account was already an admin on $SPWeb.URL" -Foregroundcolor red
}
}
}
}

No comments:

Post a Comment