Programmatically modify SharePoint Group Members – add and subtract

If you’ve ever wanted to allow admins of your farm a quick way to add a user to a SharePoint group, or a few groups, this is the script to do it.

 

Now, this script assumes you have a Forms Based Authentication solution in place, or at least have FBA as the only means to access the site.  It allows you to add the users to the various SharePoint groups, provided you have site collection admin, or higher level access to the site.

 

You could modify the groups to affect, from what is shown here, to the actual group names of different groups.  For example, say you didn’t want to add them to the Approvers and Designers SharePoint groups, then you could change those SharePoint group names.  These are the group names that you see in your site when looking at site permissions.  The script does not have any error handling, so if you try to add the user to a SharePoint group that does not exist, the script is gonna err out.

If you wanted to remove those users and add them to the Visitors group, assuming you had added the user to the Approvers group and the Designers SharePoint group, then you could run this PowerShell to remove them and add them to the Visitors group.

Now, in order to run either of these two powershell scripts, you need to be logged into the Front End SharePoint server, have an account with either Farm Admin access, full control on the web application user policy, or site collection admin, or full control on the site that you’re targeting.

 

When you run the command, it’s gonna ask you for the URL, you need to input the full URL, so http://site.sitecolleciton.com, not site.sitecollection.com.  Then just have to enter the FBA user name.

 

You could modify this for a site that does not use FBA by changing the prefix used in the lines that read

$FBAUserNameProper = “i:0#.f|fbamembershipprovider|” + $FBAUserName

to something like this

$FBAUserNameProper = “UPN\” + $FBAUserName

where upn is what your users type in, e.g. contoso\JoeUser, the contoso\ portion of that name.

Have a Happy and Safe New Year!

 

Set-SPUser TechNet