Before we begin, we need to make sure what type of PnP module had been installed in your machine. To check please execute the following command in PowerShell
In case you receive any error like “SharePointPnpPowershell module couldn’t find in this computer," then you need to download and install the latest version of PnP Module. To install the latest version please execute the below mentioned command to download and install directly from the PowerShell gallery
Open Powershell in Administrator mode and execute the below command,
- Set-Executionpolicy remotesigned
-
- #For SharePoint Online
- Install-Module SharePointPnPPowerShellOnline
-
- #For SharePoint 2016
- Install-Module SharePointPnPPowerShell2016
-
- #For SharePoint 2019
- Install-Module SharePointPnPPowerShell2019
Here I am using SharePoint Online, so I installed SharePoint Online PnP module as you can see in my first image of this article. Once everything got installed then we are ready to connect the SharePoint online using PnP PowerShell.
- #To connect SharePoint Online
- Connect-PnPOnline -Url https:
Here I am using –UseWebLogin to authenticate my credentials through browser interface to overcome the MFA Authentication.
Once it got connected, just execute the below command to create a SharePoint Group,
- New-PnPGroup -Title "Custom"
On the above command, you can provide some more parameters like owner, Description, AllowRequestTojoinLeave. In this case, I didn't give the owner's name while creating the group but it will consider who is executing this script as an owner and it will add it in property bag.
And by default, it will add the SharePoint Group owner ID as a first entry. To check you can execute the below command to list out the users who are already added in that group,
- Get-PnPGroupMembers -Identity "Custom"
If the above command returns a successful result then execute the below command to add users to the SharePoint group,
Once the command passes to another line execute the get-PnPGroupMembers command to make sure that user ID is added successfully,
So this is the way to add users to the respective sharepoint group and if you want to remove person from the SharePoint group then you can follow the below command to do that.
Then if you are looking to delete the SharePoint Group, then the below command will help you to do so,
- Remove-PnPGroup -Identity "Custom"
We can perform aditional activities like add/change the SharePoint Group permissions. The below given command will add a permission group to your sharepoint group. In this case my SharePoint Group is already created with Read permission, so by executing the below command it will make my sharepoint group as Read+Contribute permission level.
- Set-PnPGroupPermissions -Identity "Custom" -AddRole Contribute
If you want to remove the exisitng group permission and add new permission level then the below command will help you though,
- Set-PnPGroupPermissions -Identity "Custom" -RemoveRole "Read" -AddRole "Full Control"
Just consider, you are using one Sharepoint list with unique permission and there you want change the permission level for any sharepoint group. Then you can add the parameter called -List to declare your Sharepoint list name,
- Set-PnPGroupPermissions -Identity "Custom" -List "CustomList" -RemoveRole "Read" -AddRole "Contribute"