For services such as SharePoint Online, Microsoft offers a PowerShell module for basic site administration.
To supplement this module's default functionality, community members worked with Microsoft together to release the Patterns and Practices (PnP) PowerShell module for more control than the administration modules with fewer complications.
What is the PnP PowerShell module?
The original version of the PnP PowerShell module -- named SharePoint PnP PowerShell Online -- only managed SharePoint Online and SharePoint on-premises products and required Windows PowerShell to operate. Microsoft developed the PnP PowerShell module to work with cross-platform PowerShell and expanded its capabilities beyond SharePoint. The new module has more than 600 cmdlets to manage SharePoint Online, Microsoft Teams, Microsoft Planner and Microsoft Flow. This consolidation effort attempts to further simplify the work involved with installing and working with separate PowerShell administration modules Microsoft provides to manage each service.
Why use the PnP PowerShell module?
The Microsoft SharePoint Online Services module remains the de facto management tool for that service with basic commands to connect, add, update and remove items such as site collections, sites and libraries.
PnP PowerShell contains most of the cmdlets in the SharePoint administration module but also features many new commands. PnP PowerShell does not supersede the SharePoint Online Services module but offers some improvements when using PowerShell to manage SharePoint Online.
Challenge without PnP PowerShell
For example, to connect to a SharePoint site collection and iterate all sites using PowerShell, you need to connect to SharePoint Online and then retrieve all site collections. Before PnP Powershell, you needed to use client-side object model (CSOM) references and libraries in PowerShell scripts to iterate through subsites. Using CSOM is often involved and requires extensive commands, creation of new connections and credential objects, and loading each level from the site collection to the subsites. The code is prone to connection issues and often returns 403 forbidden errors.
The PnP PowerShell module aims to resolve these issues by creating new, enhancing existing or combining functions and CSOM code for a more streamlined approach to PowerShell scripting to manage SharePoint Online.
Get started with the PnP PowerShell module
Step 1: Remove legacy PnP module (just in case)
Before the PnP.PowerShell
module, things were done using its predecessor SharePointPnPPowerShellOnline
(which is not maintained since around March 2021 and now seen as legacy).
Therefore, if you just started your journey with PnP PowerShell, you can directly jump to step 2. Otherwise please ensure to remove SharePointPnPPowerShellOnline
by using the following PowerShell command:
Uninstall-Module SharePointPnPPowerShellOnline -AllVersions
Step 2: Install the PnP PowerShell module
Not the most difficult part ;-). Just use the following PowerShell command to install teh PnP.PowerShell module:
Install-Module PnP.PowerShell
Step 3: Grant access to the desired tenant
Then final step is to grant access to the tenant for the PnP Management Shell Multi-Tenant AAD App. This is done by executing the following command:
Register-PnPManagementShellAccess
You will be prompted to log in and provide consent for your tenant.
NOTE: In case you are not a global admin, use the following command to generate a consent-URL and contact a global administrator.
Register-PnPManagementShellAccess -ShowConsentURL
Step 4: verify the installation! :-)
Now you should be equipped to enjoy the benefits of PnP.PowerShell! To test it, use the following cmdlet to connect to a site of your tenant:
#Connect to PnP Online
Connect-PnPOnline -Url "https://[YOURTENANT].sharepoint.com/" -Interactive
#Get all lists from the specified URL
Get-PnPList
And that is how it looks like if you succeeded ;-)
Conclusion
I am just starting with PnP.PowerShell but it already makes my daily life easier. Being able to create SharePoint Lists in my demo-tenant and then abeing able to easily export and import them into another tenant is pretty handy. So it brings definiitely a great benefit in terms of how to deal with the structures in SharePoint Online and makes a lot of migration-scenarios easier accessible.