Sunday, November 23, 2014

Powershell- Add or Remove User from Sharepoint Group

The below sample adds / removes list of user from a sample file.

Sample File: C:\Scripts\user.txt


domainname\user1
domainname\user2
domainname\user3


ADD USERS

$site = new-object Microsoft.SharePoint.SPSite("http://siteurl")
$web = $site.OpenWeb()

$group=$web.Groups["GroupName"]

$users=Get-Content C:\Scripts\user.txt

foreach ($user in $users) {

   write-host "adding user " $user  "to group "  $group
   $temp = $web.EnsureUser($user)
   $group.AddUser($temp)
  
}



REMOVE USERS

$site = new-object Microsoft.SharePoint.SPSite("http://siteurl")
$web = $site.OpenWeb()

$group=$web.Groups["GroupName"]

$users=Get-Content C:\Scripts\user.txt



foreach ($user in $users) {

   write-host "removing  user " $user  " from group "  $group
   $temp = $web.EnsureUser($user)
   $group.RemoveUser($temp)
  

No comments:

Post a Comment