Use PowerShell to Create SharePoint Groups

Simple enough, needed to create a few SharePoint groups and thought PowerShell would be the best answer.


$SiteUrl = "http://sharepointed.com/sites/a"
$Web = Get-SPWeb $SiteUrl

$description = “Super cool stuff”
$permissionLevel = “Read”

$groups = “Group A”, “Group B”, “Group C”

foreach($groupName in $groups)
{
$web.SiteGroups.Add($groupName, $web.SiteUsers[“domain\SomeUser”], $web.SiteUsers[“domain\SomeUser”], $description)
$group = $web.SiteGroups[$groupName]
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
$roleDefinition = $web.Site.RootWeb.RoleDefinitions[$permissionLevel]
$roleAssignment.RoleDefinitionBindings.Add($roleDefinition)
$web.RoleAssignments.Add($roleAssignment)
$web.Update()
}

$web.SiteGroups.Add(name, owner, default user, description)

Parameters
name
Type: System.String
A string that represents the new group name.
owner
Type: Microsoft.SharePoint.SPMember
An SPMember object that specifies the owner.
defaultUser
Type: Microsoft.SharePoint.SPUser
An SPUser object that specifies the default user for the group.
description
Type: System.String
A string that contains a description for the group.

More details:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spgroupcollection.add.aspx

To get all the uses / groups from a site or web see this post:

http://www.sharepointed.com/2016/10/17/get-all-groups-and-users-in-a-site-collection-or-web/

 

4 thoughts on “Use PowerShell to Create SharePoint Groups

  1. This looks great! Thanks! However what if I need to create a new group inside 50 different sites. How would I modify this script?

  2. Mike, I haven’t tested it with o365.

    You could grab the sites from an input array then do something like this. If you get real stuck, let me know and I’ll try it with o365.

    rough example:
    $someSites = “http://siteA”, “http://siteB”, “http://siteC”, “http://siteD”

    foreach($site in $someSites)
    {
    $SiteUrl = $site.URL
    $Web = Get-SPWeb $SiteUrl

    $description = “Super cool stuff”
    $permissionLevel = “Read”

    $groups = “Group A”, “Group B”, “Group C”

    foreach($groupName in $groups)
    {
    # group work here
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *