How to add a New User to SharePoint Group using Powershell Script

Steps
  1. Start your windows PowerShell on your computer.
  2. Right click and select Run as administrator option.
  3. Paste the below script on the PowerShell window and click the enter button.
  4. Check your SharePoint site Feature will activated successfully.
  1. if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)   
  2. {   
  3.     Write-Host "Connect Sharepoint cmd-Let"   
  4.     Add-PSSnapin Microsoft.SharePoint.PowerShell   
  5. }  
  6.   
  7. $url = "http://gauti.sharepoint.com"  
  8. $site = new-object Microsoft.SharePoint.SPSite($url)   
  9. $web = $site.OpenWeb()   
  10. $groups = $web.sitegroups   
  11.    
  12. $userName = "username\Domain"   
  13. write-host "--------------"   
  14. $i = 0;   
  15. $myGroups = @();   
  16. foreach($group in $groups) {   
  17.   if($group -match "admin")
  18.     {   
  19.         $myGroups += $group   
  20.     }   
  21. }   
  22.    
  23. foreach ($gr in $myGroups)    
  24. {   
  25.     write-host $gr   
  26. #add user to SP Group  
  27.     Set-SPUser -Identity $userName -web $url -Group $gr   
  28.     $theGroup = $web.SiteGroups[$gr]       
  29.     $theUser = $web.AllUsers.Item($userName)   
  30.   
  31. #Remove user from SP Group  
  32. #   $theGroup.RemoveUser($theUser);   
  33.     write-host "User " $userName   "added to " $gr   
  34. }  
Run the script with the required administrator privilege.To verify that, go the group chec user added or not.
Thanks For reading.
Ebook Download
View all
Learn
View all