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"?>
    <Group name="Approvers" description="">
    <Group name="HR" description="Team responsible for company Human Resources">

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[$] -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($, $web.CurrentUser, $null, $_.description)

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

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

#Dispose of Web and Site objects

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.


  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.

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

  3. Thanks Phil for all your great job.

  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]

  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?

  6. Hi!

    How to add permissions in the code?

  7. Hi,

    How to add Read Permission level to the New Group

  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


  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

  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

  11. Digital massage therapy chairs assist to boost
    the body’s blood stream flow, which possesses a few benefits.

  12. I’m gone to inform my little brother, that he
    should also pay a quick visit this website on regular basis to obtain updated from latest news.

  13. Hi there colleagues, its fantastic article regarding educationand completely defined,
    keep it up all the time.

  14. 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....

  15. Dreary Day

    It was a dreary day here yesterday, so I just took to messing around on the internet and found