As a SharePoint developer, you should know how to work with PnP PowerShell to connect to SharePoint Online. You need to follow certain steps to install PnP PowerShell module to work with SharePoint Online sites.
The steps are as follows:
- What is PnP PowerShell and its uses?
- Installing the PnP PowerShell Module
- Registering the Entra ID Application
- Connecting and Authenticating
Introduction to PnP PowerShell
Patterns and Practices(PnP) are a set of 700 commands the PowerShell module provides. It is specially designed for working with Microsoft 365 environments and products such as SharePoint Online, Microsoft Teams, Microsoft Planner, Microsoft Power Platform, Microsoft Entra, Microsoft Search, and more.
It helps administrators and developers to interact, automate, and configure the SharePoint and Microsoft 365 environments more efficiently.
PnP PowerShell Uses
The PnP PowerShell provides a set of easy-to-use commands that help you perform common tasks without manually clicking through SharePoint or Microsoft 365 interfaces.
Let’s take an example: As a developer working with large applications, you’re required to create 10 SharePoint lists with 15 to 20 columns per list. Completing this task manually by opening the SharePoint site and clicking around to create lists and columns will take almost half or a full day.
However, with less code, you can do the same task programmatically with PnP PowerShell within a few minutes.
- It provides various commands for managing SharePoint sites, lists, libraries, and content types.
- Manage Microsoft 365 users and groups directly with PowerShell.
- Reduce task completion time: Complete tasks faster than doing them manually.
- Less code and easy to understand: Commands are shorter than scripts in other languages.
- Automate the tasks: You can schedule scripts to run at specific times.
- Ability to develop custom cmdlets and scripts to enhance PowerShell functionality.
Check out SharePoint List Items CRUD Operations Using PnP PowerShell
Install the PnP PowerShell Module
We can run PnP PowerShell in the Windows PowerShell ISE editor or Visual Studio Code. Before you install PnP PowerShell, you may need to check if the legacy PnP PowerShell module is present.
Uninstall the Legacy SharePointPnPPowerShellOnline Module
Here is the command to uninstall the legacy SharePointShell Online module.
Uninstall-Module -Name "SharePointPnPPowerShellOnline" -AllVersions -Force
PnP PowerShell version check
Here is the cmd to check the version!
Get-InstalledModule -Name "PnP.PowerShell"
The message below will be displayed if you’re newly installing the PnP PowerShell module. Otherwise, it displays the version of the PnP PowerShell module installed in your system.
Uninstall PnP PowerShell
The command below will uninstall PnP PowerShell.
Uninstall-Module PnP.PowerShell -AllVersions
Update PnP PowerShell
Below is the command to update the PnP PowerShell
Update-Module PnP.PowerShell
or
Update-Module PnP.PowerShell -Scope CurrentUser
Install New PnP PowerShell Module
Provide the below command in the TERMINAL pane of the Visual Studio code and click on enter.
Note: You can also run the below commands on other editors, such as Windows PowerShell ISE and PowerShell 7(x64).
Install-Module PnP.PowerShell
or
Install-Module -Name "PnP.PowerShell"
After clicking enter, you’ll get the confirmation message shown below. Provide A for [Yes to All].
Now, provide the below command in the TERMINAL to get the commands from PnP PowerShell.
Get-Command -Module PnP.PowerShell
Refer to the image below; all PnP PowerShell commands are displayed after running the above command.
This way, we can install the PnP PowerShell module. Now, to interact with SharePoint or other Microsoft 365 environments, we need to register an Entra ID application. Follow the below section to register for an Entra ID application.
Check out Customizing SharePoint List Fields with PnP PowerShell
Register an Entra ID Application to Use With PnP PowerShell Script
We can register an Entra ID Application automatically or manually. However, to do this, we need at least the Application Developer or Global Administrator permission roles.
The application developer permission role is enough to register an Entra ID application, but the global administrator permission role is required to provide permissions to the app.
Automatically Register an Entra ID App for PNP PowerShell Interactive Login
Note: Ensure you run the below command from an account with access to create App IDs in your Entra ID.
Run the below command to register an Entra ID application.
Register-PnPEntraIDAppForInteractiveLogin -ApplicationName "PnP PowerShell with SharePoint" -Tenant [your tenant].onmicrosoft.com -Interactive
Once you run the above PnP PowerShell cmdlet, you’ll get the Client ID like the one below!
In the above image, you’ll see the WARNING: No permissions specified; using default permissions. Run the code below to add permissions while registering the Entra ID app.
Register-PnPEntraIDAppForInteractiveLogin -ApplicationName "PnP Rocks" SharePointDelegatePermissions "AllSites.FullControl" -Tenant [yourtenant].onmicrosoft.com -Interactive
According to your requirements, you can provide the below permissions in place of SharePointDelegatePermissions in the above cmdlet. Here are some of the permissions:
- GraphApplicationPermissions
- GraphDelegatePermissions
- SharePointApplicationPermissions
Manually Register an Entra ID App for Interactive Login
Follow the steps below to register an Entra ID app manually:
1. Navigate to Entra ID Portal and log in with your credentials. Then, click Identity in the left navigation -> Applications ->App registrations.
2. Click on the +New registration, which is located at the top.
3. Provide a name in the Name field for your app registration -> Click Register.
4. Then, the Application (client) ID will be generated. Copy this ID somewhere; we need to use this to connect PnP PowerShell with the application.
5. Then, in the left navigation under Manage -> Click on Authentication -> Click on +Add a platform -> Then, the Configure platforms panel will open, -> Select Mobile and desktop applications.
6. Provide the below link in the Custom redirect URIs and click on Configure.
http://localhost
Make sure the link is http, not https.
7. In the left navigation, click on API permissions -> Select three dots of Microsoft Graph(1) -> Click Remove all permissions.
Then, one dialog box will appear, and click Yes Remove.
8. Click on +Add a permission under Configured permissions.
9. Then, the Request API permissions panel will open -> Scroll down and select SharePoint.
10. Click on Delegated permissions -> Expand AllSites and check the AllSites.Read or any other permission according to your requirement and click on Add permissions at the bottom.
Like in the example below.
11. Click on Grant admin consent for MSFT Technologies, and then your permission status will be updated as Granted for MSFT Technologies.
Note: MSFT Technologies is my company name here, so you'll get your company name in place of this.
This way, we can automatically and manually register an Entra ID application.
Check out Create SharePoint List & Add Columns Using PnP PowerShell
Connect to SharePoint Online Using PnP PowerShell
In PnP Power Shell Connect-PnPOnline, cmd connects with SharePoint Online. Let’s look at the syntax:
Connect-PnPOnline -Url "https://contoso.sharepoint.com" -ClientId 6c5c98c7-e05a-4a0f-bcfa-0cfc65aa1f28 -Interactive
Here,
- ClientId = Provide your registered application ID.
- Interactive = Allows to log in while running the code.
- Url = Provide the URL to connect.
Let’s look at some examples of how PnP PowerShell interacts with SharePoint Online.
Example1: Get all SharePoint sites present in the provided tenant using PnP PowerShell
The Get-PnPTenantSite command is used in PnP PowerShell to get all SharePoint sites. Run the code below to get all SharePoint sites:
$ClientID = "7eaac5e7-6245-4ad3-a902-81886d9a938c"
Connect-PnPOnline -Url "https://[tenant name]-admin.sharepoint.com/" -ClientId $ClientID
Get-PnPTenantSite
Here,
- $ClientID = Provide the application ID/client ID you get after registering an Entra ID application.
- -Url = Provide the SharePoint admin URL to get all sites.
You can see all the sites in the provided SharePoint tenant in the TERMINAL pane.
Example 2: Get all SharePoint lists present from a particular SharePoint site.
To get all the lists in the SharePoint list, we need to use the Get-PnPList command. Run the code below, and you’ll see all the lists on the SharePoint site.
$ClientID = "7eaac537-6345-4ad3-a932-81865d9a938c"
Connect-PnPOnline -Url "https://[tenant name].sharepoint .com /sites/SalesDepartmentTea m" -ClientId $ClientID -Interactive
#Get All Lists
Get-PnPList
Example 3: Get all the items present in a particular SharePoint list.
The Get-PnPListItem command gets all the items in a SharePoint list. Run the PnP PowerShell code below to get the list items.
$ClientID = "7eaac5e7-6345-4ad3-a902-81885d9a938c"
$ListName ="Task Tracker"
Connect-PnPOnline -Url "https://[tenant name].sharepoint.com/sites/SalesDepartment Team" -ClientId $ClientID -Interactive
#Get All List items
Get-PnPListItem -List $ListName
- $ListName = Provide the SharePoint list name.
We’ve been getting all sites, lists, and items from SharePoint. Read permissions are enough to do this. To create the SharePoint lists and items, we require Edit permissions. Based on the actions, we require permission to achieve this through PnP PowerShell.
This way, we can interact with SharePoint Online using PnP PowerShell.
I hope you understand PnP PowerShell and its uses, how to install it, register an Entra ID application, and how to connect with SharePoint Online using PnP PowerShell. In this article, I clearly explained how to register an EntraID application automatically and manually.
Follow this article if you’re new to PnP PowerShell and are facing issues connecting PnP PowerShell with SharePoint. Recently, only Microsoft updated the multi-authentication for security purposes.
You may also like the following PowerShell tutorials:
- Get Microsoft 365 Group Members Using PowerShell
- Delete All SharePoint List Items using PowerShell
- PowerShell try catch with examples
- The term ‘get-spweb’ is not recognized as the name of a cmdlet function PowerShell SharePoint error
- How to connect to Microsoft Teams using PowerShell
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com
The commands are not working as expected.
When I tried
Connect-PnPOnline –Url https://xxx.sharepoint.com –Credentials (Get-Credential)
It asked me to uninstall all previous versions of “SharePointPnPPowerShellOnline” and install “PnP.PowerShell” like below
Uninstall-Module -Name SharePointPnPPowerShellOnline -AllVersions -Force
Install-Module -Name PnP.PowerShell
Then I again tried
Connect-PnPOnline –Url https://xxx.sharepoint.com –Credentials (Get-Credential)
Connect-PnPOnline : AADSTS65001: The user or administrator has not consented to use the application with ID ‘31359c7f-bd7e-475c-86db-fdb8c937548e’
named ‘PnP Management Shell’. Send an interactive authorization request for this user and resource.
Thank You. It worked for me
Great content! Keep up the good work!