3

Has anyone successfully used PS to create groups and assign a owner (another SP group not a domain user). The scripts I've found online, I can't get them to work. Just wondering if anybody has any working scripts to share. Preferable create from csv or txt so I can create a template.

5

This is something we built in-house:

function Add-SPGroup {
<#
.SYNOPSIS
    Use Add-SPGroup to create a SharePoint Group.
.DESCRIPTION
    This function uses the Add() method of a SharePoint RoleAssignments property in an SPWeb to create a SharePoint Group.
.EXAMPLE
    Add-SPGroup -webUrl "http://sharepoint.local" `
                -groupName "Test Group" `
                -description "My Group" `
                -ownerName "DOMAIN\userlogin" `
                -defaultMemberName "DOMAIN\userlogin" `
                -membership "DOMAIN\userlogin, DOMAIN\userlogin2"`

#>
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$true)][string]$webUrl,
    [Parameter(Mandatory=$true)][string]$groupName,
    [Parameter(Mandatory=$true)][string]$description,
    [Parameter(Mandatory=$true)][string]$ownerName,
    [Parameter(Mandatory=$true)][string]$defaultMemberName,
    [Parameter(Mandatory=$true)][string]$membership
)
try {
    $codeLocation = "Opening web and checking group does not already exist."
    $web = Get-SPWeb $webUrl;
    $spGroup = $web.SiteGroups[$groupName];

    if ($spGroup -ne $null) {
        Write-Warning "Group $groupName already exists!";  
        return $spGroup; 
    }
    $codeLocation = "Setting owner/member details.";
    $owner = $web.EnsureUser($ownerName);
    $member = $web.EnsureUser($defaultMemberName);

    $codeLocation = "Creating group.";
    $web.SiteGroups.Add($groupName, $owner, $member, $description);
    $spGroup = $web.SiteGroups[$groupName];

    $codeLocation = "Adding users to Group"
    $usersToAdd = $membership.split(",");
    foreach($user in $usersToAdd) {
        $ensuredUser = $web.EnsureUser($user.trim());
        $spGroup.AddUser($ensuredUser);
    }

    $web.RoleAssignments.Add($spGroup);
    $web.Dispose();
    return $spGroup;
}
catch {
    Write-Warning $codeLocation;
    Write-Error $Error[0];
} }

Hope this helps

1

I have a custom function adapted from this StackOverflow question. For whatever reason, the group owner is not settable if you are using the New-SPOSiteGroup commandlet.

Function Set-GroupOwner{
    <#
        .SYNOPSIS
        Sets the specified GroupOwner, a Sharepoint group, as the owner of the specified Group
        .DESCRIPTION    
        When creating groups with New-SPOSiteGroup, there is no way to set the group owner. While -Owner is a parameter of Set-SPOSiteGroup,
        it does not appear to be settable. This function solves that problem.
        .PARAMETER SiteUrl
        The Url of the subsite the group needs permission to.
        .PARAMETER GroupToUpdate
        The name of the group that needs updated.
        .PARAMETER GroupOwner
        The SharePoint group that should be listed as the group owner.
        .EXAMPLE
        This example sets the Eric Playground Owners group as the group owner of A Test Subsite Members
        Set-GroupOwner -SiteUrl "https://tenant.sharepoint.com/teams/eric" -GroupToUpdate "A Test Subsite Members" -GroupOwner "Eric Playground Owners"
        .NOTES
        Adapted from https://stackoverflow.com/a/25654742/318776
        .LINK
        https://stackoverflow.com/a/25654742/318776
    #>
    Param(
        [Parameter(Mandatory=$true,HelpMessage="The URL of the site collection",Position=0)][ValidateNotNull()]
        [string]$SiteUrl,

        [Parameter(Mandatory=$true,HelpMessage="The group that needs updated",Position=1)][ValidateNotNull()]
        [string]$GroupToUpdate,

        [Parameter(Mandatory=$true,HelpMessage="The desired group owner",Position=2)][ValidateNotNull()]
        [string]$GroupOwner

    )
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)

    $owner = $context.Web.SiteGroups.GetByName($GroupOwner)
    $group = $context.Web.SiteGroups.GetByName($GroupToUpdate)

    $group.Owner = $owner
    $group.Update()
    $context.ExecuteQuery()

    $context.Dispose()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.