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
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.
- $credentials= Get-Credential
- $siteurl="https://<tenant-name>. sharepoint.com"
- Connect-PnPOnline -Url $siteurl -Credentials $credentials
Create 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.
- Connect-MsolService -Credential $credentials
- New-MsolUser -UserPrincipalName "ravishankar@<tenant-name>.onmicrosoft.com" -DisplayName "Ravishankar" -FirstName "Ravi" -LastName "Shankar"
Create 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.
- Owner - User Login Name. If not assigned, current user login name is assigned by default.
- Description - Group Description
The following snippet helps to create a new SharePoint group on SharePoint site.
- $newGroup = New-PnPGroup -Title "Hubfly Group" -Owner "ravishankar@<tenant-name>. onmicrosoft.com" -Description "Hubfly Group"
Set Permissions to SharePoint Group
The group permissions can be created using Set-PnPGroupPermissions command on SharePoint sites. The required parameters are,
- Identity - Give group name to set permission
- 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.
- $AddRole="Edit"
- Set-PnPGroupPermissions -Identity $newGroup -AddRole $AddRole
Add User to SharePoint Group
The user can add to the SharePoint Group by using following code snippets.
- $web=Get-PnPWeb
- $ctx= $web.Context
- $newGroupName=$web.SiteGroups.GetByName("Hubfly Group")
- $ctx.Load($newGroupName)
- $ctx.ExecuteQuery()
- $userName="ravishankar@<tenant-name>. onmicrosoft.com"
- $userInfo = $web.EnsureUser($userName)
- $ctx.Load($userInfo)
- $addUser = $newGroup.Users.AddUser($userInfo)
- $ctx.Load($addUser)
- $ctx.ExecuteQuery()
Final Code
- #connect to site
- $credentials= Get-Credential
- $siteurl="https://<tenant-name>. sharepoint.com"
- Connect-PnPOnline -Url $siteurl -Credentials $credentials
- #Add New User
- Connect-MsolService -Credential $credentials
- New-MsolUser -UserPrincipalName "ravishankar@<tenant-name>.onmicrosoft.com" -DisplayName "Ravishankar" -FirstName "Ravi" -LastName "Shankar"
- #Create New Group
- $newGroup = New-PnPGroup -Title "HubflyGroup" -Owner "ravishankar@<tenant-name>. onmicrosoft.com" -Description "Hubfly Group"
- #Assign Permission
- $AddRole="Edit"
- Set-PnPGroupPermissions -Identity $newGroup -AddRole $AddRole
- #Add User to the Group
- $web=Get-PnPWeb
- $ctx= $web.Context
- $newGroupName=$web.SiteGroups.GetByName("HubflyGroup")
- $ctx.Load($newGroupName)
- $ctx.ExecuteQuery()
- $userName="ravishankar@<tenant-name>. onmicrosoft.com"
- $userInfo = $web.EnsureUser($userName)
- $ctx.Load($userInfo)
- $addUser = $newGroup.Users.AddUser($userInfo)
- $ctx.Load($addUser)
- $ctx.ExecuteQuery()
- #Disconnect from site
- Disconnect-PnpOnline
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.