Create SharePoint Groups with Group Members in Microsoft SharePoint Server 2010

Introduction

This script can create SharePoint groups with group members in Microsoft SharePoint Server 2010.

Scenarios

In a real world, you may need to create a lot of SharePoint groups and add many users to this group for some reasons. If there are lots of users, it will be a lot of work for IT Administrators, so they need a script to help them complete this task.

Script

This script contains one advanced function: New-OSCSPSiteGroup. You can use this script in the following way.

1. Open SharePoint 2010 Management Shell with a  proper account that has enough permission to manage SharePoint site.

2. Run Import-Module cmdlet to import this module file.
Import-Module filepath\scriptname.psm1

Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

PowerShell
Edit|Remove
#Verify user specified permission level$permissionLevelNames = @() 
$spWeb.RoleDefinitions |Foreach-Object {$permissionLevelNames+$_.Name} 
$permissionLevelNamesString = $permissionLevelNames-join ","if ($permissionLevelNames-notcontains $PermissionLevel) { 
    $warningMsg = $Messages.InvalidPermissionLevel 
    $warningMsg = $warningMsg-$PermissionLevel,$permissionLevelNamesString$pscmdlet.WriteWarning($warningMsg) 
} else { 
    $verboseMsg = $Messages.PermissionLevelPrompt 
    $verboseMsg = $verboseMsg-$spGroup,$PermissionLevel$pscmdlet.WriteVerbose($verboseMsg) 
    #Get role definition item$spRoleDefinition = $spWeb.RoleDefinitions.Item($PermissionLevel)                 
    #Create a role assigment object$spRoleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($spGroup) 
    $spRoleAssignment.RoleDefinitionBindings.Add($spRoleDefinition) 
    $spWeb.RoleAssignments.Add($spRoleAssignment) 
}
Examples

Example 1: How to display help about New-OSCSPSiteGroup
To display help about this function, run this command.
Get-Help New-OSCSPSiteGroup -Full

Example 2: How to create a new SharePoint group with group members. 
To create a new SharePoint group with group members, please run this command.
New-OSCSPSiteGroup -SiteURL "http://server_name/sites/sitename" -Name "GroupName" -Description "description" -Owner "domain\johnd" - Member "domain\johnd","domain\janed","domain\managers" -Verbose

Example 3: How to create a new SharePoint group with permission level. 
To create a new SharePoint group with permission level, please run this command.
New-OSCSPSiteGroup -SiteURL "http://server_name/sites/sitename" -Name "GroupName" -Description "description" -Owner "domain\johnd" -PermissionLevel "Contribute" -Verbose

Note: This script allows you to use custom permission levels, but you cannot create a SharePoint group with an invalid permission level.

Additional Resources

Technical Resources: 
Windows PowerShell Advanced Function
Microsoft.SharePoint.SPWeb class
Microsoft.SharePoint.SPRoleAssignment

Forum Threads: 
http://social.technet.microsoft.com/Forums/eu/sharepoint2010setup/thread/453ab8d3-f046-45bf-9571-4d01a995038a
http://powergui.org/thread.jspa?threadID=16284