Written by

Ravishankar

Share

  

Category

see all categories
intranet as a service

Creating SharePoint Group, User And Add User To The Group On SharePoint Site Using PnP PowerShell

In this article, we are going to see how to create a SharePoint user and SharePoint group and add the created user to the SharePoint group using PnP PowerShell. The Client-Side Object Model (CSOM) is used internally for these operations.

Prerequisite

Before you begin utilizing PowerShell to oversee SharePoint Online, ensure that the SharePoint Online Management Shell is installed. You can install the SharePoint Online Management Shell by downloading and running the SharePoint Online Management Shell. You only need to do this once for each computer from which you are running SharePoint Online PowerShell commands.

Connect to Site

Before connecting to the SharePoint site, we need to get credentials by using the Get-Credential cmdlet which creates a credential object for a specified username and password. You can use the credential object in security operations. By default, an authentication dialog box appears to prompt the user like the below example picture

credntial pnp site

Then connect to SharePoint site using Connect-PnPOnline cmdlet. The required parameters are -Url and -Credential . In -Url Parameter passes the site URL and in -Credential passes the Get-Credential.

The following code snippet helps to connect SharePoint site.

Code 1Create SharePoint User

The Users can be created by using New-MsolUser command on SharePoint sites. The New-MsolUser cmdlet created a user in a SharePoint site in order to give the user access to services. The required parameters are,

  • UserPrincipalName - Specifies email addresses for the user.
  • DisplayName - Specifies the display name of the user.
  • FirstName - Specifies the first name of the user.
  • LastName - Specifies the last name of the user.

The following snippet helps to create SharePoint user on SharePoint site.

Code 2Create SharePoint Group

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

1.  Owner - User Login Name. If not assigned, current user login name is assigned by default..
2. Description - Group Description
Leaderboard_CDR

The following snippet helps to create a new SharePoint group on SharePoint site.

Code 3

Set Permissions to SharePoint Group

The group permissions can be created using Set-PnPGroupPermissions command on SharePoint sites.The required parameters are,

1. Identity - Give group name to set permission

2. AddRole – Assign permission level for the group

Default permission levels are,

  • Read - Read permissions to the SharePoint site.
  • Edit - Edit permissions to the SharePoint site.
  • Full Control - Full Control permissions to the SharePoint site
  • View Only - View Only permissions to the SharePoint site.

The following snippets help to set permission to new SharePoint group on SharePoint site.

Code 4

 Add User to SharePoint Group

The user can add to the SharePoint Group by using following code snippets.

Add User to SharePoint Group

Final Code

FInal Code

We have covered how to create SharePoint groups, users and add users to the SharePoint groups programmatically using PnP-PowerShell commands. PnP-PowerShell is supported by SharePoint Online. The operations mentioned above are tested on SharePoint Online environments.