Thursday, July 4, 2013

powershell add bulk users to sharepoint site

Here is a simple PowerShell script code to add multiple users to SharePoint site

Function AddUsers($web)
{
    $AccountList = @("Domain\user1","Domain\user2")
    
    # Get the site owners group from web
    foreach($Group in $web.SiteGroups)
    {
        if($Group.Name -like "*Owner*")
        {
           $MyGrp = $web.SiteGroups[$Group.Name] 
        }
    }

    #Add the array users to Site owners group.
    foreach ($Account in $AccountList)
      {
        
        $user1 = $web.Site.RootWeb.EnsureUser($Account)
        $MyGrp.AddUser($user1)
        Write-host "$Account was added as admin " $web.Url -Foregroundcolor DarkGreen
      }
}


Thank you !!


No comments:

Post a Comment