top of page
Writer's pictureNakkeeran Natarajan

Working With SharePoint Groups Using PnP PowerShell


Introduction

In this article, we will learn how we can create, retrieve, update and delete SharePoint groups, using PnP PowerShell. The Client Side Object Model is used internally for these operations.


Prerequisite

You need to have PowerShell 3.0 available on a Windows machine. You need to install or import PnP PowerShell packages. You can download the installers, or view more documentation from the official site. The installers are available here. Online version installer is preferred for On Premise or Office 365 operations. You can also install all the three installers for testing (SharePoint 2013, 2016, online).


The PnP PowerShell is supported by SharePoint 2013 On Premise and Office 365 versions. The following operations are tested on SharePoint 2013 and Office 365 environments.


Connect To Site

Connect to the site using the snippet, given below. PnP PowerShell code, given below, helps in getting the current context of the site, using the Client Side Object Model (CSOM).

  1. # Get Current Context Site

  2. $siteurl = "https://abc.sharepoint.com"

  3. Connect-SPOnline -Url $siteurl  

  4. $ctx = Get-SPOContext  

The URL, to access the site group manually, is http://siteurl/_layouts/15/groups.aspx


Create SharePoint Group

The groups can be created using New-SPOGroup command on SharePoint sites. The required parameters for creating a group is title. Other parameters can also be added. 

  • Owner – User Login Name. If not assigned, current user login name is assigned by default.

  • Description

  • AllowMembersEditMembership

  • AllowRequestToJoinLeave

  • AutoAcceptRequestToJoinLeave

  • OnlyAllowMembersViewMembership

  • RequestToJoinEmail

The following code snippet helps creating a new SharePoint group on SharePoint site.

  1. New-SPOGroup -Title "TestGroup" -Description "Test group from PnP PowerShell" -AllowRequestToJoinLeave -AutoAcceptRequestToJoinLeave  


Retrieve SharePoint Groups

The groups can be created in SharePoint sites using PnP PowerShell. 


The code snippet given below helps in retrieving all the SharePoint groups. The properties of each group can be accessed by using foreach loop.

  1. $groups = Get-SPOGroup  foreach($group in $groups){      

  2. Write-Host "Title             : " $group.Title      

  3. Write-Host "Description       : " $group.Description      

  4. Write-Host "Can Members edit? : " $group.AllowMembersEditMembership      

  5. Write-Host "Owner             : " $group.OwnerTitle      

  6. Write-Host "------------------------------------------" }  

The following code snippet shows the retrieving of single group information. The identity parameter is required, where group title is passed to retrieve information.

  1. $group = Get-SPOGroup -Identity "TestGroup"

  2. Write-Host "Title             : " $group.Title  

  3. Write-Host "Description       : " $group.Description  

  4. Write-Host "Can Members edit? : " $group.AllowMembersEditMembership  

  5. Write-Host "Owner             : " $group.OwnerTitle  


Update SharePoint Group

The SharePoint group can be updated using Set-SPOGroup command. The required parameter for updating a group is identity. Other parameters that can be updated are the parameters shown in the create group section. In addition to that, the following parameters can be updated. The role (permissions) values can be edit and full control. 

  • AddRole

  • RemoveRole

The following code helps in updating group information.

  1. Set-SPOGroup -Identity "TestGroup" -Title "TestGroup1" -Description "Test group updated" -AddRole "Edit" -Owner "Nakkeeran" In addition, update can also be done using the below snippet. Set-SPOGroupPermissions -Identity "TestGroup" -AddRole "Edit"


Delete SharePoint Group

The SharePoint group can be deleted using Remove-SPOGroup command. The required parameter for deleting a group is identity. The title is passed as value and as identity. Force attribute is used to delete the group without confirmation prompts.

  1. Remove-SPOGroup -Identity "TestGroup" -Force  

Summary

Thus, you have learned how to retrieve, create, update or delete SharePoint groups programmatically using PnP PowerShell commands. PnP PowerShell is supported by SharePoint 2013 On Premise and Office 365 versions. The operations mentioned above, are tested on SharePoint 2013 and Office 365 environments.

0 comments

Comments


bottom of page