Thursday, 9 February 2017

Adding Users to all the WebApplication in the SharePoint Farm

###########################################################################
#Script to add a user to all the Web Application for a SharePoint Farm
###########################################################################

#Adding SharePoint SnapIn - you can run it from normal PowerShell Console (Always use Run as Administrator)
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue


#Collect User Details like UserID, Permission and DisplayName and store them in a Variable
$user = Read-host "Enter User Id for the user you want to add to all the web application like (i:0#.w|Domain\UserId) "
$displayName = Read-Host "Enter the Name of the User (Lastname,FirstName) "
$Perm = Read-host "Provide the permission you want to provide (FullControl/FullRead) "

#Adding Users to all the WebApplication in the SharePoint Farm
Get-SPWebApplication | foreach {
 
    $webApp = $_
    $policy = $webApp.Policies.Add($user, $displayName)
    $policyRole = $webApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::$Perm)
    $policy.PolicyRoleBindings.Add($policyRole)
    $webApp.Update()
    Write-host "Added User $user to Web Application $($Webapp.url)" -ForegroundColor Green
}