none
How to create user groups and add users in sharepoint 2010 site collection using powershell RRS feed

  • Question

  • Hi All,
    Iam new in SharePoint powershell. I am trying to create groups and add users in that group for a site collections so that later on i cann add for other sites too. Please let me know the way using powershell scripts to create groups and add users.
     Also please let me know the steps to follow while following your answer as I am new to this. thanks for your time.

    Regards
    Thursday, June 30, 2011 9:02 PM

Answers

  • Hi,

    I have given the complete working code.  I tested in my environment.. working fine. (Powershell script to create Groups and Users)

    XML

     

    <?xml version="1.0"?>
    <Groups>
     <Group name="TestApprovers" description="">
      <Users>
       <User>Domain\UserName</User>
       <User>Domain\UserName</User>
      </Users>
     </Group>
    </Groups>
    
    

    PowerShell

     

     

    Function AddSnapIn()
    {
     #handles exceptions caused by trying to add a snapin
    	Trap [Exception]
    	{
    	  continue; 
    	}
    
     #Check that the required snapins are available , use a comma delimited list.
     #example
     # ("Microsoft.SharePoint.PowerShell", "Microsoft.Office.Excel")
    	$RequiredSnapIns = ("Microsoft.SharePoint.PowerShell");
    	ForEach ($SnapIn in $RequiredSnapIns)
    	{
    		if ( (Get-PSSnapin -Name $SnapIn -ErrorAction SilentlyContinue) -eq $null ) 
    		{ 
    		 Add-PsSnapin $SnapIn
    		} 
      else 
      {
      }
    	}
     [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
    }
    
    AddSnapIn;
    
    #Get Site and Web objects
    $site = Get-SPSite “http://XXX”
    $web = $site.RootWeb 
    
    #Get XML file containing groups and associated users
    $groupsXML = [xml] (Get-Content ("Users.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()
    

     Due credit to this blog..

    http://get-spscripts.com/2010/07/creating-multiple-sharepoint-groups-and.html

    Regards

    Ranga


    Regards Ranga www.sharepointfrontier.com
    • Marked as answer by Seven M Monday, July 4, 2011 8:44 AM
    Friday, July 1, 2011 3:33 PM
  • Hi,

      I just finished this script by adding another function to add permission levels on the groups. I have posted this script. Thanks to Ranga and Authors of all the Script from different Blogs. here is the full script. thanks..

     


    Function AddSnapIn()
    {
     #handles exceptions caused by trying to add a snapin
     Trap [Exception]
     {
       continue;
     }

     #Check that the required snapins are available , use a comma delimited list.
     #example
     # ("Microsoft.SharePoint.PowerShell", "Microsoft.Office.Excel")
     $RequiredSnapIns = ("Microsoft.SharePoint.PowerShell");
     ForEach ($SnapIn in $RequiredSnapIns)
     {
      if ( (Get-PSSnapin -Name $SnapIn -ErrorAction SilentlyContinue) -eq $null )
      {
       Add-PsSnapin $SnapIn
      }
      else
      {
      }
     }
     [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
    }

    AddSnapIn;

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

    #Get XML file containing groups and associated users
    $groupsXML = [xml] (Get-Content ("c:\scripts\powershell\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($_, "", "", "")
     }
    }
    function AddGroupToSite ($web, $groupName, $permLevel)
    {
        $account = $web.SiteGroups[$groupName]
        $assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
        $role = $web.RoleDefinitions[$permLevel]
        $assignment.RoleDefinitionBindings.Add($role);
        $web.RoleAssignments.Add($assignment)
    }

    AddGroupToSite -web $web -groupName "Hrrrr" -permLevel "Full Control"
    AddGroupToSite -web $web -groupName "designersss" -permLevel "Read"

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

    Regargs

    Singh

     

    Wednesday, July 6, 2011 5:47 PM

All replies

  • Hi,

    Please find similar type of thing done here : http://get-spscripts.com/2010/07/creating-multiple-sharepoint-groups-and.html

    Regards

    Ranga


    Regards Ranga www.sharepointfrontier.com
    Friday, July 1, 2011 8:43 AM
  • Hi Ranga,

      Thanks for replying back. I tried this script. I created one xml and save the xml content and changed the domain name and user name and saved it. Then i created a new .ps1 file where i saved the the powershell script and changed the xml path and sp site url.  Then i run this script in sharepoint management shell as : powershell -command c:\scipts\new.ps1 (the script file.) .When i run it, it thorws me error as follows:

    1) The term get-spsite is not recognized as the name of a cmdlet, function,script file or operable program.Check teh name of the file or path included, verify that the path is correct and try again.

      Also the other errors iam getting sre same for all null variable and _ space of the scripts are as follows:-

    2)cannot index into a null array at c:\scripts\groupusers.ps1:11 char:25+ if($web.sitegroups [<<<<<$_.name] -eq $null) + categoryinfo   : Invalidoperation: (Approvers:string) []. runtime exception + fullyQualifiedErrorid: nullArray.

    The 2 line error is repeatitive 4 more times. Please hel me as i dont understand these errors.. Thankll a lot..

     

    Regards

    Friday, July 1, 2011 2:38 PM
  • Hi,

    I have given the complete working code.  I tested in my environment.. working fine. (Powershell script to create Groups and Users)

    XML

     

    <?xml version="1.0"?>
    <Groups>
     <Group name="TestApprovers" description="">
      <Users>
       <User>Domain\UserName</User>
       <User>Domain\UserName</User>
      </Users>
     </Group>
    </Groups>
    
    

    PowerShell

     

     

    Function AddSnapIn()
    {
     #handles exceptions caused by trying to add a snapin
    	Trap [Exception]
    	{
    	  continue; 
    	}
    
     #Check that the required snapins are available , use a comma delimited list.
     #example
     # ("Microsoft.SharePoint.PowerShell", "Microsoft.Office.Excel")
    	$RequiredSnapIns = ("Microsoft.SharePoint.PowerShell");
    	ForEach ($SnapIn in $RequiredSnapIns)
    	{
    		if ( (Get-PSSnapin -Name $SnapIn -ErrorAction SilentlyContinue) -eq $null ) 
    		{ 
    		 Add-PsSnapin $SnapIn
    		} 
      else 
      {
      }
    	}
     [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
    }
    
    AddSnapIn;
    
    #Get Site and Web objects
    $site = Get-SPSite “http://XXX”
    $web = $site.RootWeb 
    
    #Get XML file containing groups and associated users
    $groupsXML = [xml] (Get-Content ("Users.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()
    

     Due credit to this blog..

    http://get-spscripts.com/2010/07/creating-multiple-sharepoint-groups-and.html

    Regards

    Ranga


    Regards Ranga www.sharepointfrontier.com
    • Marked as answer by Seven M Monday, July 4, 2011 8:44 AM
    Friday, July 1, 2011 3:33 PM
  • Hi Ranga,

    Thank... you so much for your script..i have no words to say, really. This time it worked after adding your snapin function in the script.

    I just have 1 last question for you regarding this script. I see groups and their users in the site collection and but dont have any permission levels(its none).Please let me know how and where to put permisiion thing in the script? it will be super-perfect after adding permission.

    Once again Thank You So Much. I wont do it without your support.

     

    Regards

    SinghSaab


    Friday, July 1, 2011 7:16 PM
  • Hi,

      I just finished this script by adding another function to add permission levels on the groups. I have posted this script. Thanks to Ranga and Authors of all the Script from different Blogs. here is the full script. thanks..

     


    Function AddSnapIn()
    {
     #handles exceptions caused by trying to add a snapin
     Trap [Exception]
     {
       continue;
     }

     #Check that the required snapins are available , use a comma delimited list.
     #example
     # ("Microsoft.SharePoint.PowerShell", "Microsoft.Office.Excel")
     $RequiredSnapIns = ("Microsoft.SharePoint.PowerShell");
     ForEach ($SnapIn in $RequiredSnapIns)
     {
      if ( (Get-PSSnapin -Name $SnapIn -ErrorAction SilentlyContinue) -eq $null )
      {
       Add-PsSnapin $SnapIn
      }
      else
      {
      }
     }
     [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
    }

    AddSnapIn;

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

    #Get XML file containing groups and associated users
    $groupsXML = [xml] (Get-Content ("c:\scripts\powershell\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($_, "", "", "")
     }
    }
    function AddGroupToSite ($web, $groupName, $permLevel)
    {
        $account = $web.SiteGroups[$groupName]
        $assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
        $role = $web.RoleDefinitions[$permLevel]
        $assignment.RoleDefinitionBindings.Add($role);
        $web.RoleAssignments.Add($assignment)
    }

    AddGroupToSite -web $web -groupName "Hrrrr" -permLevel "Full Control"
    AddGroupToSite -web $web -groupName "designersss" -permLevel "Read"

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

    Regargs

    Singh

     

    Wednesday, July 6, 2011 5:47 PM
  • Is it possible to do this with CSV instead of XML? I want to incorporate this into an SSIS package and exporting from SQL to XML is tricky.

    Wednesday, October 31, 2012 5:27 PM
  • Hi,

    I am getting error after executing your code

     

    Index operation failed; the array index evaluated to null.

    At D:\CreateSPGroup_.ps1:39 char:22

    + if ($web.SiteGroups[ <<<< $_.Name] -eq $null)

    + CategoryInfo : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : NullArrayIndex

    Kindly let me know the solution for this


    Regards Vinit Shah

    Thursday, November 28, 2013 9:07 AM
  • Hi Bhupinderjit,

    How you have resolve the below error as i am facing same error, i have also include snapin function

    "2)cannot index into a null array at c:\scripts\groupusers.ps1:11 char:25+ if($web.sitegroups [<<<<<$_.name] -eq $null) + categoryinfo   : Invalidoperation: (Approvers:string) []. runtime exception + fullyQualifiedErrorid: nullArray" error


    Regards Vinit Shah


    • Edited by Vinit Shah Thursday, November 28, 2013 9:21 AM
    Thursday, November 28, 2013 9:19 AM
  • Hi Guys,

    I have return complete blog with PowerShell Code and XML for above problem.

    Please refer.

    http://insidesharepoint2016.blogspot.com/2015/12/add-user-to-sharepoint-group-in-each.html

    Tuesday, January 5, 2016 4:51 PM