Tag Archives: SharePoint hosting India

SharePoint 2013 Hosting – ASPHostPortal.com :: How to Add or Remove A New User to SharePoint Group Using Powershell Script

How to Add or Remove A New User to SharePoint Group Using Powershell Script

In SharePoint site you can see usually a lot of groups. Sometimes administrator gets a task to add user to several groups. If it’s 3-5 groups you can do it manually, but if you need to add user to 30 groups? I’m too lazy to do it manually and too smart not do it at all.

SharePoint 2013 Hosting

  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.

So let’s start out PowerShell ISE and connect SharePoint CMD-let:

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
{ 
 Write-Host "Connect Sharepoint cmd-Let" 
 Add-PSSnapin Microsoft.SharePoint.PowerShell 
}

Let’s enter site collection url, username for inserting to groups and get array of destination SharePoint groups. For example, let this groups contain a string “admin”

$url = "http://user"
$userName = "user\testuser1"
$site = new-object Microsoft.SharePoint.SPSite($url) 
$web = $site.OpenWeb() 
$groups = $web.sitegroups
write-host "------get array of groups which contain a string "admin"-----" 
$myGroups = @(); 
foreach($group in $groups) { 
if($group -match "admin") { 
$myGroups += $group 
}

Go through each element of array and add user to SharePoint group

foreach ($gr in $myGroups) { 
#add user to SP Group
 Set-SPUser -Identity $userName -web $url -Group $gr 
 write-host "User " $userName "added to " $gr 
}

And if you made a false and, for example, entered wrong username, you can easily remove user from SharePoint groups with this PS-code:

foreach ($gr in $myGroups) { 
 $theGroup = $web.SiteGroups[$gr] 
 $theUser = $web.AllUsers.Item($userName) 
#Remove user from SP Group
 $theGroup.RemoveUser($theUser); 
 write-host "User " $userName " removed from " $gr
}

If you don’t want to collect these pieces here’s a full version:

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
{ 
 Write-Host "Connect Sharepoint cmd-Let" 
 Add-PSSnapin Microsoft.SharePoint.PowerShell 
}
$url = "http://user"
$site = new-object Microsoft.SharePoint.SPSite($url) 
$web = $site.OpenWeb() 
$groups = $web.sitegroups 
$userName = "user\testuser1" 
write-host "--------------" 
$i = 0; 
$myGroups = @(); 
foreach($group in $groups) { 
 if($group -match "admin") { 
 $myGroups += $group 
 } 
} 
foreach ($gr in $myGroups) { 
 write-host $gr 
#add user to SP Group
 Set-SPUser -Identity $userName -web $url -Group $gr 
 $theGroup = $web.SiteGroups[$gr] 
 $theUser = $web.AllUsers.Item($userName) 
#Remove user from SP Group
# $theGroup.RemoveUser($theUser); 
 write-host "User " $userName "added to " $gr 
}

If there are non-english letters in a group name or if the group you need is on another site (but in this site collection), then you can have error on Set-SPUser command. In this case I can advice you to use another command $web.SiteGroups[group name].AddUser(user)

foreach ($gr in $myGroups) { 
 Write-Output "Группа: $gr " 
#alternative way to add user to SP Group 
 $theUser = $web.AllUsers.Item($userName)
 $web.SiteGroups[$gr].AddUser($theUser) 
 Write-Output "User $userName added to $gr" 
}

Cheap and Recommended SharePoint 2013 Hosting

ASPHostPortal.com is Perfect, suitable hosting plan for a starter in SharePoint. ASPHostPortal the leading provider of Windows hosting and affordable SharePoint Hosting. ASPHostPortal proudly working to help grow the backbone of the Internet, the millions of individuals, families, micro-businesses, small business, and fledgling online businesses. ASPHostPortal has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch, ASPHostPortal guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability.