Introduction to PnP PowerShell

In this segment, we are going to show you “How to Use PnP PowerShell.”

PnP PowerShell (Patterns and Practices PowerShell) is a set of cmdlets designed for SharePoint Online and SharePoint On-Premises environments. Developed by the SharePoint PnP community, it simplifies the management and automation of various SharePoint tasks, allowing administrators and developers to efficiently manage their SharePoint environments.

Recommended Articles

What is PnP PowerShell?

PnP PowerShell is an open-source library of PowerShell cmdlets. It provides a more intuitive and powerful set of commands than the standard SharePoint Online Management Shell. PnP PowerShell cmdlets are designed to work with SharePoint Online (Office 365) and SharePoint 2013, 2016, and 2019 On-Premises.

Key Features

  • Simplified Scripting: PnP PowerShell cmdlets offer a simpler syntax compared to traditional SharePoint cmdlets, making it easier to write and maintain scripts.
  • Extensive Command Set: It includes commands for a wide range of operations, including site collection management, list and library management, user and group management, and more.
  • Cross-Platform: PnP PowerShell is compatible with Windows, macOS, and Linux, making it versatile for different development environments.
  • Community-Driven: Developed and maintained by the SharePoint PnP community, it continuously evolves with new features and improvements based on community feedback.

Installing PnP PowerShell

To get started with PnP PowerShell, you need to install it using PowerShell. Here’s how to install it from the PowerShell Gallery:

  • Open PowerShell: Run PowerShell as an administrator.
  • Install PnP PowerShell:
Install-Module -Name PnP.PowerShell
  • Update PnP PowerShell: If you already have it installed and want to update to the latest version:
Update-Module -Name PnP.PowerShell

PnP PowerShell itself isn’t an enterprise app you directly register with your tenant. Instead, it leverages an Azure AD application for authentication. There are two main approaches:

Register PnP PowerShell Management Application with Tenant

Method-1. Use the existing PnP Management Shell application:

This multi-tenant app is already available and offers a simplified approach. You can grant access to it using the Register-PnPManagementShellAccess cmdlet in PnP PowerShell.

Here’s what you’ll do:

  • Open PowerShell with PnP PowerShell installed and run the below command.
Register-PnPManagementShellAccess.

Optionally, you can:

  • Use -ShowConsentUrl to get a URL for granting consent if you don’t have admin rights. Share this URL with someone who can grant access for your organization.

Method-2. Register your own Azure AD application:

This method provides more control over permissions. You’ll need to create an app registration in Azure AD and then configure PnP PowerShell to use it.

  • Follow the instructions for Register-PnPAzureADApp in PnP PowerShell documentation [PnP Register Azure AD App ON GitHub pnp.github.io]
  • This cmdlet can also generate a self-signed certificate for your app registration.

Connecting to SharePoint

Before executing any PnP PowerShell cmdlets, you need to connect to your SharePoint environment. Here’s an example of how to connect to a SharePoint Online site:

Connect-PnPOnline -Url https://yourtenant.sharepoint.com/sites/yoursite -Interactive

Basic Usage Examples of PnP Powershell

1. Retrieving Site Collections:

Get-PnPTenantSite

2. Creating a New List:

New-PnPList -Title "New List" -Template GenericList

3. Adding a User to a SharePoint Group:

Add-PnPUserToGroup -LoginName user@domain.com -Identity "Group Name"

4. Uploading a File to a Document Library:

Add-PnPFile -Path "C:\path\to\file.txt" -Folder "Shared Documents"

5. Exporting and Importing a Site Template:

Get-PnPProvisioningTemplate -Out "template.xml" 
Apply-PnPProvisioningTemplate -Path "template.xml"

Conclusion

PnP PowerShell is an essential tool for SharePoint administrators and developers looking to streamline their workflows and manage their SharePoint environments more effectively. With its extensive command set, ease of use, and cross-platform support, PnP PowerShell can significantly enhance your SharePoint management and automation tasks. Whether you are new to SharePoint or an experienced professional, PnP PowerShell offers the tools you need to optimize your SharePoint operations.

Did you find us helpful?

Support us on Patreon, follow us on Facebook, & subscribe to YouTube.

Harish Kumar

As a founder of the PcMac YouTube channel and website, Our goal is to provide Free Technical help to people and spread knowledge to everyone.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments