Add user to sharepoint group

26/11/2013 12:41

function AddUserToGroup

{

#Add user(s) to sharepoint security group

param($webUrl, [array]$users, $groupName)

 

"=================================="

$script:MyInvocation.MyCommand.Name

"=================================="

$web = Get-SPWeb $webUrl

$group = $web.SiteGroups[$groupName]

if($group -and $users)

{

foreach($u in $users)

{

$userName = $env:UserDomain + "\" + $u

$user = $null

$user = $web.Site.RootWeb.EnsureUser($userName)

if($user)

{

"Adding '{0}' to '{1}' group..." -f $userName, $groupName

Set-SPUser -Identity $user –Web $web -Group $groupName

$web.Update()

}

}

 

}

$web.Dispose()

}

$admins = @("jan.tomas", "lukas.sevcik")

$webUrl = "https://externalsp2013/sites/grouptest"

$groupName = "Group Test Owners"

AddUserToGroup -webUrl $webUrl -users $admins -groupName $groupName