Add Users in Bulk to SharePoint Groups


The other day I had a user ask to add 600+ users to a SharePoint group. Immediately I knew PowerShell was going to save the day, again. So, here is the script I wrote. You can download the full script along with a sample answer file. All I ask is that you let me know if you run into issues, and know that this script is provided as-is with no warranty or guarantees. I haven’t taken into account all possible errors so my error handling is very limited here.

**Typically it is recommended you use an Active Directory security group, but sometimes we have to make exceptions**

Don’t freak out because of the amount of code. Most of the code is building the menu to make it easy for even the least experienced PowerShell user to use. I write a lot of PowerShell scripts, so I don’t like having to open a script and review it to determine what/how to run it. Yes, I could build a help menu for the script, but I prefer a menu that will simply give me and others on my team the options to choose from. The screenshots below should help you understand what the code is doing. The help menu will bring you to this article.

STEP 1

  • Run the file named Add-SPUser_Group.ps1 from the SharePoint CLI
  • Enter a path to a directory to store the activity log (see below)

Add-SPUser_Group_Log

STEP 2

  • Enter A at the menu (see below)

Add-SPUser_Group_Menu

STEP 3

  • Enter the path and filename of the .csv answer file (example answer file included in script download)
  • Enter the URL of the site to add the users to
  • Enter the Group ID

You can find the Group ID of a SharePoint Group by navigating to the Group in the browser. The ID is in the URL of the group

Add-SPUser_Group_Input

STEP 4

  • The list of Usernames will be displayed to you (Usernames must be in DOMAIN\Username format in your answer file) (see below)

Add-SPUser_Group_UserListing

STEP 5

  • You will see one of the following messages for each user in your answer file
    • “DOMAIN\Username” created and added to “Group ID” (displayed if a user account did not exist in the site. The account is added to the site, then added to the group)
    • “DOMAIN\Username” has been added to “Group ID” (displayed if a user already exists in the site and is added to the group)

STEP 6

  • You will now be brought back to the main menu shown in STEP 2

Download Code

#Region Action Function
#Action taken via user input based on menu option chosen
Function Action ($result)
{	
	if ($result -eq "A")
	{
		AnswerCreate
	}
	if ($result -eq "B")
	{

	}
	if ($result -eq "?")
	{
		clear
		#Opens Default Internet browser and navigates to the below site that holds the instructions for this program
		Start-Process -FilePath "https://techchucker.wordpress.com/2013/09/17/addbulkspusergroups/"
	}
}
#endRegion

#Region PressKey Function

#User must enter any key to continue
Function PressKey
{
	Write-Host "Press any key to continue..." -ForegroundColor Black -BackgroundColor White

	$x = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp,AllowCtrlC")

	return clear
}
#endRegion

#Region WebInput Function
#Function to request web input from user
Function WebInput
{
	$inpWeb = Read-Host "Enter the URL of the site to add users to a group"

	return $inpWeb
}
#endRegion

#Region GroupInput Function
#Function to request SharePoint Group input from user
Function GroupInput
{
	$inpGroup = Read-Host "Enter the SharePoint Group ID to add users to"

	return $inpGroup
}
#endRegion

#Region AnswerCeate Function

#Function to take answer file and add multiple users to SharePoint Group
Function AnswerCreate
{
	clear

	#Imports user inputted answer file
	$userList = Import-Csv ($answerFile = Read-Host "Enter path to Answer File (e.g. c:\filename.csv)")
	$web = WebInput
	$group = GroupInput

	Write-Output $userList
	PressKey

	#Iterates through each record storing variables and executing user add
	ForEach ($user in $userList)
	{
		$validateUser = $null
		$LANID = $user."LANID"

		$validateUser = Get-SPUser -Identity $LANID -Web $web -ErrorAction SilentlyContinue #This will throw an error if the user does not exist

		if($validateUser -eq $null)
		{
			Write-Host $LANID "does not exist"
			New-SPUser -UserAlias $LANID -Web $web -group $group
			Write-Host $LANID "created and added to " $group
		}
		else
		{
			#Adds user/s to the SharePoint group
			Set-SPUser -Identity $LANID -Web $web -Group $group	
			Write-Host $LANID "has been added to "$group	
		}
	}	
}
#endRegion

#Region Menu Function

#Function to display the menu for the program
Function Menu
{
	Write-Host "Add Users to SP Groups in Bulk`n`n" -ForegroundColor Black -BackgroundColor White
	Write-Host "Choose the action you would like to perform shown below.`n`n"

	Write-Host "	A	-	Add users in bulk using Answer File`n"
	Write-Host "	?	-	Program Help`n"
	Write-Host "	Exit	Exits this program`n`n"
}
#endRegion

#Region Program Actions
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"

$logPath = Read-Host "Enter log path (c:\Logs)"
$date = Get-Date -UFormat %y%m%d.%H.%M.%S
$logFile = "\" + $date + "_AddUserGroup.txt"

Start-Transcript -path $logPath$logFile -append

do
{
	clear
	Menu
	$result = Read-Host "Enter your selection"
	if($result -ne "exit")
	{
		Action $result
		#Write-Host "Would you like to return to the main menu?`n`n"
		#$confirm = Confirm ($inp = Read-Host "Enter Y or N")
		$confirm = $True
	}
	else
	{
		$confirm = $false
	}
}
while($confirm -eq $True)

Stop-Transcript
#endRegion
Advertisements

2 thoughts on “Add Users in Bulk to SharePoint Groups

  1. Hello,

    Just tried this and it works great.

    I do have a question.

    How can I make this that it works for external users? I see that this script reads users from the AD, but what if I want to add external users?

    Appreciate your time and help.

    Regards,

    Richard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s