Subscribe:

Labels

Wednesday, August 17, 2011

How to create sharepoint group using Powershell script

01. function New-SPGroup {
02<#
03.Synopsis
04    Use New-SPGroup to create a SharePoint Group.
05.Description
06    This function uses the Add() method of a SharePoint RoleAssignments property in an SPWeb to create a SharePoint Group.
07.Example
08    C:\PS>New-SPGroup -Web http://intranet -GroupName "Test Group" -OwnerName DOMAIN\User -MemberName DOMAIN\User2 -Description "My Group"
09    This example creates a group called "Test Group" in the http://intranet site, with a description of "My Group".  The owner is DOMAIN\User and the first member of the group is DOMAIN\User2.
10.Notes
11    Name: New-SPGroup
12    Author: Ryan Dennis
13    Last Edit: July 18th 2011
14    Keywords: New-SPGroup
15.Link
16
17http://www.rameshsps2010.blogspot.com/
18
20
21.Inputs
22    None
23.Outputs
24    None
25#Requires -Version 2.0
26#>
27    [CmdletBinding()]
28    Param(
29    [Microsoft.SharePoint.PowerShell.SPWebPipeBind]$Web,
30    [string]$GroupName,
31    [string]$OwnerName,
32    [string]$MemberName,
33    [string]$Description
34    )
35    $SPWeb = $Web.Read()
36    if ($SPWeb.SiteGroups[$GroupName] -ne $null){
37        throw "Group $GroupName already exists!"
38    }
39    if ($SPWeb.Site.WebApplication.UseClaimsAuthentication){
40        $op = New-SPClaimsPrincipal $OwnerName -IdentityType WindowsSamAccountName
41        $mp = New-SPClaimsPrincipal $MemberName -IdentityType WindowsSamAccountName
42        $owner = $SPWeb | Get-SPUser $op
43        $member = $SPWeb | Get-SPUser $mp
44    }
45    else {
46    $owner = $SPWeb | Get-SPUser $OwnerName
47    $member = $SPWeb | Get-SPUser $MemberName
48    }
49$SPWeb.SiteGroups.Add($GroupName, $owner, $member, $Description)
50$SPGroup = $SPWeb.SiteGroups[$GroupName]
51$SPWeb.RoleAssignments.Add($SPGroup)
52$SPWeb.Dispose()
53return $SPGroup
54}

No comments:

Post a Comment