Thursday, 1 July 2010

Creating multiple SharePoint groups and adding users with PowerShell

One of the great features in PowerShell is the ability to read an XML file and traverse the DOM nodes using a very simple dot notation. An example is the script below, which uses an XML file to modify and create SharePoint groups, adding their corresponding users. First the XML file:

<?xml version="1.0"?>
<Groups>
    <Group name="Approvers" description="">
        <Users>
            <User>DOMAIN\pchilds</User>
            <User>DOMAIN\tsmith</User>
        </Users>
    </Group>
    <Group name="HR" description="Team responsible for company Human Resources">
        <Users>
            <User>DOMAIN\jdavies</User>
            <User>DOMAIN\pjackson</User>
        </Users>
    </Group>
</Groups>

In this scenario, we are not just creating new groups, but also adding users to existing groups (“Approvers” in the example above). If the users already exists in a group, the script will just continue and add the next one:

#Get Site and Web objects
$site = Get-SPSite “http://portal”
$web = $site.RootWeb

#Get XML file containing groups and associated users
$groupsXML = [xml] (Get-Content ("C:\Path\Groups.XML"))

#Walk through each group node defined in the XML file
$groupsXML.Groups.Group | ForEach-Object {
    #Check to see if SharePoint group already exists in the site collection
    if ($web.SiteGroups[$_.name] -eq $null)
    {
        #If the SharePoint group doesn't exist already - create it from the name and description values at the node
        $newGroup = $web.SiteGroups.Add($_.name, $web.CurrentUser, $null, $_.description)
    }

    #Get SharePoint group from the site collection
    $group = $web.SiteGroups[$_.name]

    #Add the users defined in the XML to the SharePoint group
    $_.Users.User | ForEach-Object {
        $group.Users.Add($_, "", "", "")
    }
}

#Dispose of Web and Site objects
$web.Dispose()
$site.Dispose()

The script connects to the <Group /> node defined in the XML file with the line $groupsXML.Groups.Group and then uses the ForEach-Object statement to walk through each one. It detects if the group already exists – if not it creates a new one using the name and description defined at the node – and then walks through each <User /> node to add the users. Note the way the dot notation is used to access various parts of the XML node tree and how the “$_” syntax is used to call the node in context and its associated values.

15 comments:

  1. Thanks
    Some improvements - in SP2010 instead of $group.Users.Add you can use $group.AddUser(...), it must work more faster, if you are going to add many users.

    ReplyDelete
  2. Can you tell me where created the groups by using this script?
    I did not see in site permission list.

    ReplyDelete
  3. Thanks Phil for all your great job.

    ReplyDelete
  4. One update on line 2:
    $site = Get-SPSite “http://portal”

    This works great as long as the site is the Root Site Collection of WebApp. If you have site collections within the WebApp, you cannot add them using name - for example

    $site = Get-SPSite “http://portal/managedpath/sitecollection2” *does not work*

    Instead you can get the Site Collection ID and use the script as follows (no quotes or brackets):

    $site = Get-SPSite [SiteID_GUID]

    ReplyDelete
  5. You are using a built-in SharePoint Permission Level. Can a new group be added to a custom permission level at [group] creation time?

    ReplyDelete
  6. Hi!

    How to add permissions in the code?

    ReplyDelete
  7. Hi,

    How to add Read Permission level to the New Group

    ReplyDelete
  8. Replies
    1. I love how you still write and share about your day and experiences! You feel like such a real, nice, and humble person because of this!

      What Is SEO / Search Engine Optimization? | Master Of Seo


      Delete

  9. that may be the end of this write-up. Here you will obtain some web pages that we assume youll enjoy,
    just click the links over
    Here My Website :

    - Mesin Es Krim -
    - Mesin Es Krim Mrah

    ReplyDelete
  10. we like to honor lots of other internet web sites around the internet, even when they arent linked to us,
    by linking to them. Under are some webpages worth checking out
    Here My Website :

    - Sewa Mobil -
    - Mobil Jakarta
    - Rental Mobil Jakarta

    ReplyDelete