How to Install the PnP PowerShell Module for SharePoint Online?

What is PnP PowerShell? The new PnP PowerShell module PnP.PowerShell is a cross-platform, .net framework-based PowerShell product that can run on any operating system that supports .net core, like Windows, Linux, macOS, etc., and provides 500+ cmdlets to work with Microsoft 365 environment (No support for On-Premises server products, As a side note!) cloud products like SharePoint Online, Microsoft Teams, Microsoft Planner, Security & Compliance, Azure Active Directory, and more. If you are administering Microsoft SharePoint, you know that PnP PowerShell is an essential tool for managing your environment. It’s an open-source component and community-provided library with active community members providing support, so there won’t be any SLA or direct support from Microsoft. This article will show you how to install the PnP PowerShell module for SharePoint Online.

To install the new PnP PowerShell module, You have to follow these steps:

  1. Uninstall the Legacy SharePointPnPPowerShellOnline Module if installed already.
  2. Install the New PnP PowerShell Module.
  3. Register Azure AD Application and Grant Access to the tenant.

Let’s get started! Open the Windows PowerShell console as an Administrator and execute these steps to install PnP.PowerShell module.

install pnp powershell sharepoint online

Step 1: Uninstall the Legacy SharePointPnPPowerShellOnline Module

Check if the classic PnP PowerShell module is installed with the below command:

Get-Module SharePointPnPPowerShellOnline -ListAvailable | Select-Object Name,Version

This returns the Name and version of legacy PnP PowerShell installed on the machine (If any). Uninstall Any previous PnP PowerShell Modules for SharePoint Online installed:

Uninstall-Module SharePointPnPPowerShellOnline -Force -AllVersions

Step 2: Install the New PnP PowerShell Module

How do I install a new PowerShell module? To install the new PnP PowerShell module, use:

Install-Module PnP.PowerShell

How to install PnP PowerShell offline? If needed, You can also use setup files Download the PnP PowerShell Offline Installation package and install it with:

Install-Package C:\Path\File.nupkg

Step 3: Register a new Azure AD Application and Grant Access to the tenant

Register-PnPManagementShellAccess

The final step is granting the tenant access to the PnP Management Shell Multi-Tenant Azure AD Application. On executing the above cmdlet, you’ll be prompted to log in and provide consent for your tenant. You must log in with Global Admin (or Tenant Administrator) permissions and complete this step.

add permission to PnP Management Shell

In case you are not a global admin, use: Register-PnPManagementShellAccess -ShowConsentUrl and share the URL you get from this cmdlet with the Tenant Admin, and they can complete this consent step from the URL you share.

Finally, You can verify the installation by getting a list of PnP PowerShell cmdlets:

Get-Command -Module PnP.Powershell

Connect SharePoint Online site using PnP PowerShell

How do I use PnP PowerShell in SharePoint Online? Once you are done with the above steps, You can connect to SharePoint Online through PnP PowerShell scripts with username and password as:

#Connect to PnP Online
Connect-PnPOnline -Url "https://Crescent.sharepoint.com/sites/Marketing/" -Credential (Get-Credential)

#Get All Lists
Get-PnPList

Connect-PnPOnline using MFA:

To connect to SharePoint Online using PnP PowerShell MFA (Multifactor authentication): Use the “-Interactive” switch instead of “Credentials” if your account is MFA enabled. Behind the scenes, each cmdlet executes a client-side object model code to achieve functions.

#Connect to SharePoint site
Connect-PnPOnline -Url "https://Crescent.sharepoint.com/sites/Marketing/" -Interactive

#Get All Lists
Get-PnPList
If you want to schedule your PowerShell script, You can use either the Windows Task scheduler or Azure Automation! How to Schedule PowerShell Scripts with Azure Automation?

PowerShell to connect to OneDrive for Business

Microsoft OneDrive for Business – The cloud-based storage and file-sharing part of the Office 365 suite can be managed with PowerShell too. To access OneDrive for Business site from PowerShell, you will need to first connect to the OneDrive site by running the following cmdlet: Connect-PnPOnline. Once you are authenticated, you can then run any available cmdlets against your OneDrive for Business site just as you would any other SharePoint Online site. E.g., Let’s connect to OneDrive site and create a new folder:

#Parameters
$OneDriveSiteURL = "https://crescent-my.sharepoint.com/personal/salaudeen_crescent_com"
$FolderName = "Archives"
  
Try {
    #PowerShell to Connect to OneDrive for Business
    Connect-PnPOnline -Url $OneDriveSiteURL -Interactive
      
    #ensure folder in SharePoint Online using powershell
    Resolve-PnPFolder -SiteRelativePath "Documents/$FolderName"
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Update PnP PowerShell for SharePoint Online

How do I update PnP PowerShell? To update the PnP PowerShell, run:

Update-Module -Name "PnP.PowerShell"

This will download the latest PnP PowerShell module and install it on your local machine.

update pnp powershell

What version of PnP PowerShell do I have?

To check the installed version of PnP PowerShell in your system, use:

Uninstall-Module -Name "PnP.PowerShell"

Uninstall PnP PowerShell Module

If you would like to remove the PnP PowerShell, you can run:

Uninstall-Module -Name "PnP.PowerShell"

Can I have both the New PnP PowerShell and the Classic PnP Modules?

If you want to use the PnP PowerShell module for SharePoint Server (On-Premises), You can install it along with Pnp.PowerShell module as:

Install-Module -Name SharePointPnPPowerShell2019 –AllowClobber

This script installs the SharePointPnPPowerShell2019 module and makes cmdlets available from this module. You can use “Import-Module SharePointPnPPowerShell2019” at the first line to instruct your script to load and use Cmdlets from this specific module. As a side note, the old SharePointPnPPowerShellOnline module is archived and no longer updated.

At times, You may need to use the CSOM PowerShell scripts with the combination of PnP PowerShell cmdlets, as we don’t have PnP PowerShell cmdlets for each and everything in SharePoint Online: How to Connect to SharePoint online using CSOM PowerShell?

How do I get all columns, types, and internal names in my site by PnP PowerShell?

Use: Get-PnPField | Select Title, TypeDisplayName, InternalName
More info: Get Site Columns in SharePoint Online using PowerShell

How do you get all SP library IDs with PnP Online?

To get IDs of all SharePoint Online document libraries, use: Get-PnPList | Where-Object {$_.BaseType -eq “DocumentLibrary”}
More info: Get All Document Libraries in SharePoint Online

How do you create top link navigation in PowerShell?

Use the Add-PnPNavigationNode cmdlet to create a top navigation link to SharePoint Online site.
More info: Add Top Navigation Link in SharePoint Online using PowerShell

How to use SharePoint Online Management Shell?

The SharePoint Online Management Shell is a PowerShell Module that helps administrators to manage SharePoint Online sites. Just install the PowerShell Module “Microsoft.Online.SharePoint.PowerShell”, connect to SharePoint Online with Connect-SPOService, and then you can start using any available cmdlets.
More info: Connect to SharePoint Online from PowerShell

How do I install SharePointPnPPowerShellOnline module?

If you want to install the legacy SharePointPnPPowerShellOnline module for backward compatibility or to manage SharePoint On-premises versions, use: “Install-Module SharePointPnPPowerShellOnline”
More info: How to Connect to SharePoint Online using PnP PowerShell?

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

2 thoughts on “How to Install the PnP PowerShell Module for SharePoint Online?

Leave a Reply

Your email address will not be published. Required fields are marked *