Skip to content
All articlesSharePoint

PnP PowerShell for SharePoint Online: Install, Connect, Cmdlets

What PnP PowerShell is, how to install it on PowerShell 7.4, register your own Entra ID app, connect with Connect-PnPOnline, and the cmdlets admins use most.

25 Nov 202615 min read
PnP PowerShell for SharePoint Online: Install, Connect, Cmdlets

What PnP PowerShell is

PnP PowerShell is an open-source, community-supported PowerShell module with more than 700 cmdlets for Microsoft 365, including SharePoint Online. To use it you install the module in PowerShell 7.4 or later, register your own Entra ID application, and connect to a site with Connect-PnPOnline.

Microsoft's own overview describes it as "a .NET 8.0 based PowerShell Module providing over 700 cmdlets that work with Microsoft 365 environments such as SharePoint Online, Microsoft Teams, Microsoft Project, Security & Compliance, Microsoft Entra ID, and more." The project's repository now puts the count at over 900. It runs on Windows, Linux and macOS.

Two things are worth knowing before you rely on it:

  • It is a community project. Microsoft's overview says: "It is not a Microsoft-provided module so there's no SLA or direct support for this open-source component from Microsoft." Problems are reported on the project's issues list in GitHub.
  • The current module is for SharePoint Online. The project's README says the cross-platform version "will only support SharePoint Online". The original Windows-only cmdlets, which supported SharePoint 2013, 2016 and 2019 on-premises, are a separate, older module.

Getting started has three steps, and the rest of this page follows them:

  1. Install the PnP.PowerShell module.
  2. Register your own Entra ID application.
  3. Connect and sign in.

PnP PowerShell and the SharePoint Online Management Shell

SharePoint administrators usually meet two modules. They are separate, with separate sign-in commands.

PnP PowerShellSharePoint Online Management Shell
Module namePnP.PowerShellMicrosoft.Online.SharePoint.PowerShell
Published byThe open-source PnP communityMicrosoft
SupportCommunity, through GitHub issuesMicrosoft
Sign-in commandConnect-PnPOnlineConnect-SPOService
What you connect toA site, such as https://contoso.sharepoint.com/sites/projectsThe SharePoint admin center, https://contoso-admin.sharepoint.com
PowerShell versionPowerShell 7.4.0 or laterWindows PowerShell 5, or PowerShell 7 with -UseWindowsPowerShell
Operating systemsWindows, Linux and macOSWindows
Cmdlet namesGet-PnPList, Get-PnPListItem, Get-PnPFileGet-SPOSite, Set-SPOSite, Set-SPOTenant

For the Microsoft module, see Connect-SPOService: connect to SharePoint Online with PowerShell.

Step 1: Install PnP PowerShell

The installation page states the requirement in one line: "You need PowerShell 7.4.0 or later to use PnP PowerShell." Check your version with $PSVersionTable.PSVersion, then run the install command in a PowerShell 7 window.

TaskCommand
Install the latest stable releaseInstall-Module PnP.PowerShell
Update to the latest stable releaseUpdate-Module PnP.PowerShell
Install the latest nightly buildInstall-Module PnP.PowerShell -AllowPrerelease
Update to the latest nightly buildUpdate-Module PnP.PowerShell -AllowPrerelease -Force
Remove every installed versionUninstall-Module PnP.PowerShell -AllVersions
Run it in a Docker containerdocker run -it m365pnp/powershell

Two notes from the project's README:

  • Windows PowerShell 5.1 and the ISE. The README gives Install-Module PnP.PowerShell -RequiredVersion 1.12.0 -Force for people still on PowerShell 5.1 or the ISE. That pins an old release, so the steps on this page assume PowerShell 7.4.
  • The version 4 preview. It is built on .NET 10 and needs PowerShell 7.6 or later. The stable release needs PowerShell 7.4.0 and is built on .NET 8.0.

Step 2: Register your own Entra ID application

PnP PowerShell signs in through an application registered in your own Microsoft Entra ID tenant. The registration guide says: "It has always been a recommended practice to register your own Entra ID Application to use with PnP PowerShell. As of September 9th, 2024, this has become mandatory step." On that date the shared, multi-tenant PnP Management Shell Entra ID app was deleted, which is why older scripts that connected without a client ID stopped working.

Register with one cmdlet

For interactive sign-in, run this once:

Register-PnPEntraIDAppForInteractiveLogin -ApplicationName "PnP.PowerShell" -Tenant contoso.onmicrosoft.com

After the app is registered you are asked to give consent for it. Keep the application (client) ID, because every connection uses it.

The person running the cmdlet needs at least the Application Developer role or the Global Administrator role. The guide says the first "should be enough to create the application registration in Entra ID", and that Global Administrator permissions "might be needed after this to set the desired permissions on the application registration."

By default the cmdlet adds a limited set of permission scopes. To choose your own, use the -GraphDelegatePermissions, -SharePointDelegatePermissions, -GraphApplicationPermissions and -SharePointApplicationPermissions parameters.

Register by hand in the Entra admin center

The guide also documents the manual route. The settings that matter are:

SettingValue
NameThe guide recommends PnP.PowerShell, so the name matches the module
PlatformMobile and desktop applications
Redirect URIhttp://localhost - the guide stresses "this is http and NOT https"
Extra redirect URI for -OSLoginms-appx-web://microsoft.aad.brokerplugin/{client_id}
API permissionsDelegated SharePoint permissions, for example AllSites.Read under AllSites
ConsentGrant admin consent for your organization. A greyed-out button means your account lacks the rights to consent

The guide explains delegated permissions this way: "Delegated Permissions means that adding a permission level here does not mean that anyone gets those permissions." A person who signs in through the app can only do what their own SharePoint permissions allow.

Step 3: Connect with Connect-PnPOnline

Connect to the site you want to work on, with the client ID from Step 2:

Connect-PnPOnline -Url "https://contoso.sharepoint.com/sites/projects" -Interactive -ClientId <client id>

A sign-in window opens and you step through multi-factor authentication if your account uses it. Interactive is the default sign-in method. Run Get-PnPWeb afterward to confirm which site you are connected to.

Sign-in methods

MethodParametersWhat the documentation says
Interactive-Interactive -ClientId <client id>Shows a popup window to "authenticate and step through the multi-factor authentication flow"
Device login-DeviceLogin -ClientId <client id>Gives you a code to enter at microsoft.com/devicelogin on any device, including any multi-factor step
Operating system login-OSLogin -ClientId <client id>Uses Web Account Manager, with Windows Hello, conditional access policies and FIDO keys. Needs the extra redirect URI from Step 2
User name and password-ClientId <client id> -Credentials (Get-Credential)"It will not work with multi factor authentication. Therefore this method is less recommended."
Certificate file-ClientId <client id> -Tenant contoso.onmicrosoft.com -CertificatePath <path to .pfx>App-only sign-in with no person present
Certificate thumbprint-ClientId <client id> -Tenant contoso.onmicrosoft.com -Thumbprint <thumbprint>App-only, using a certificate imported into the Windows Certificate Store
Managed identity-ManagedIdentityFor Azure Functions, Azure Automation runbooks and Azure Cloud Shell

Save the client ID so you can leave it out

Typing -ClientId on every connection gets old. The documentation gives two ways to store it.

An environment variable. Name it ENTRAID_APP_ID, ENTRAID_CLIENT_ID or AZURE_CLIENT_ID; they are used in that order. On Windows:

[System.Environment]::SetEnvironmentVariable('ENTRAID_CLIENT_ID', '<client id>', [EnvironmentVariableTarget]::User)

A client ID for each tenant. Useful when you work in more than one tenant:

Set-PnPManagedAppId -Url https://contoso.sharepoint.com -AppId <client id>

This adds an entry to Windows Credential Manager, the macOS keychain or the Linux Secret Service, and Connect-PnPOnline -Url https://contoso.sharepoint.com -Interactive then picks the right client ID for the address. It is available in versions newer than 2.12.0. Add -Verbose to Connect-PnPOnline to see which client ID is used.

Tenant-wide cmdlets and the admin site

Cmdlets that work across the tenant, such as Get-PnPTenantSite, need "Access to the SharePoint Tenant Administration site", which means a SharePoint Administrator account. You do not have to connect to the admin address yourself: when -TenantAdminUrl is left out, the cmdlets "assume to connect automatically to" https://<tenant>-admin.sharepoint.com "where appropriate".

Work with two sites at once

-ReturnConnection returns a connection object and leaves your current connection alone. Pass it to other cmdlets with -Connection:

$archive = Connect-PnPOnline -Url "https://contoso.sharepoint.com/sites/archive" -Interactive -ClientId <client id> -ReturnConnection
Get-PnPList -Connection $archive

When you finish, Disconnect-PnPOnline disconnects the current connection and clears its token cache.

Connect without a person signing in

Scheduled scripts use app-only sign-in with a certificate. The authentication guide is direct about it: "Your application registration needs to use a public/private key pair certificate to authenticate. A client secret is not supported." The public key (.cer) goes into the app registration and the private key (.pfx) is used to connect.

Register-PnPEntraIDApp creates the app registration and the certificate together:

$result = Register-PnPEntraIDApp -ApplicationName "PnP.PowerShell" -Tenant contoso.onmicrosoft.com -OutPath c:\mycertificates -DeviceLogin
$result

The output shows the client ID. The cmdlet saves PnP.PowerShell.cer and PnP.PowerShell.pfx to the -OutPath folder, returns the private key certificate in base64 encoding, and shows a URL where an administrator gives consent.

Then connect with the certificate file:

Connect-PnPOnline contoso.sharepoint.com -ClientId <client id> -Tenant contoso.onmicrosoft.com -CertificatePath c:\mycertificates\PnP.PowerShell.pfx

If the .pfx has a password, add -CertificatePassword with a secure string. To use a certificate from the Windows Certificate Store, import the .pfx into Personal > Certificates and connect with -Thumbprint in place of -CertificatePath.

The cmdlets SharePoint admins use most

Each description below is taken from the cmdlet's own documentation page. Connect first; every cmdlet works on the site you connected to unless the table says otherwise.

Sites

CmdletWhat it doesExample
Get-PnPTenantSiteRetrieves site collection information. With no parameters it returns all site collections except OneDrive sites. Needs SharePoint administrator accessGet-PnPTenantSite
Get-PnPSiteReturns the current site collectionGet-PnPSite
Get-PnPWebReturns the current webGet-PnPWeb
New-PnPSiteCreates a communication site, a Microsoft 365 group-connected team site, or a modern team site with no groupNew-PnPSite -Type CommunicationSite -Title Contoso -Url https://contoso.sharepoint.com/sites/contoso

Lists and items

CmdletWhat it doesExample
Get-PnPListReturns the lists in the current webGet-PnPList
New-PnPListCreates a new listNew-PnPList -Title Announcements -Template Announcements
Get-PnPListItemRetrieves list itemsGet-PnPListItem -List Tasks
Add-PnPListItemAdds an item to a listAdd-PnPListItem -List "Demo List" -Values @{"Title" = "Test Title"; "Category"="Test Category"}
Set-PnPListItemUpdates a list itemSet-PnPListItem -List "Demo List" -Identity 1 -Values @{"Title" = "Test Title"; "Category"="Test Category"}

Files and folders

CmdletWhat it doesExample
Get-PnPFolderItemLists the files and subfolders in a folder. For a library with more than 5,000 items, use its -List parameterGet-PnPFolderItem
Get-PnPFileDownloads a file. The example saves it to the current folderGet-PnPFile -Url "/sites/project/Shared Documents/Document.docx"
Add-PnPFileUploads a local file to a folderAdd-PnPFile -Path c:\temp\company.master -Folder "_catalogs/masterpage"
Get-PnPFileVersionRetrieves the previous versions of a file, without the current versionGet-PnPFileVersion -Url Documents/MyDocument.docx

Recycle bin

CmdletWhat it doesExample
Get-PnPRecycleBinItemReturns items from the recycle bin of the site you connected to. With no parameters, the first-stage recycle binGet-PnPRecycleBinItem
Restore-PnPRecycleBinItemRestores a recycle bin item to its original location, after asking you to confirmRestore-PnPRecycleBinItem -Identity 72e4d749-d750-4989-b727-523d6726e442

People and permissions

CmdletWhat it doesExample
Get-PnPSiteCollectionAdminReturns the current site collection administratorsGet-PnPSiteCollectionAdmin
Add-PnPSiteCollectionAdminAdds one or more site collection administrators, keeping the existing onesAdd-PnPSiteCollectionAdmin -Owners "user@contoso.onmicrosoft.com"
Get-PnPGroupReturns one SharePoint group or all SharePoint groups in the siteGet-PnPGroup
Get-PnPGroupMemberRetrieves the members of a SharePoint groupGet-PnPGroupMember -Group "Marketing Site Members"

Site templates

CmdletWhat it doesExample
Get-PnPSiteTemplateGenerates a provisioning site template from a webGet-PnPSiteTemplate -Out template.pnp
Invoke-PnPSiteTemplateApplies a site template to a webInvoke-PnPSiteTemplate -Path template.xml -Url https://contoso.sharepoint.com/sites/sitename

Two reports on this site are built from these cmdlets: how to check the size of SharePoint storage and finding SharePoint files not modified in years. When the second report shows libraries full of files nobody has touched, Squirrel archives them by last-modified age, site, library, folder or file type into your own Azure Blob Storage and leaves a stub so users can still open them.

Permissions that catch people out

Connecting successfully does not mean every cmdlet will run. Three requirements are written into the cmdlet documentation:

  • Get-PnPRecycleBinItem needs a site collection administrator. Its page says: "SharePoint: Site Collection Administrator. SharePoint Tenant Admin alone is not enough". A SharePoint Administrator who is not an admin of the site connects to the tenant admin address and uses Get-PnPTenantRecycleBinItem. SharePoint site collection administrator explains how to add yourself.
  • Add-PnPSiteCollectionAdmin needs you to be a site collection admin already. Its page says: "You must be a Site Collection Admin to run this command." A SharePoint Administrator who is not one uses Set-PnPTenantSite -Owners.
  • New-PnPSite needs the right to create sites. With a signed-in user it requires "the SharePoint Administrator or Global Administrator role unless self service site creation is enabled."

The permissions on the app registration matter as well. A person who signs in through the app is limited by both the app's delegated permissions and their own SharePoint permissions.

Frequently asked questions

Is PnP PowerShell a Microsoft product?

It is an open-source library provided and supported by the community. Microsoft documents it and states that it "is not a Microsoft-provided module so there's no SLA or direct support for this open-source component from Microsoft." Issues are reported on GitHub.

Which version of PowerShell does PnP PowerShell need?

PowerShell 7.4.0 or later for the current stable release, which is built on .NET 8.0. The version 4 preview needs PowerShell 7.6 or later.

How do I install PnP PowerShell?

Open PowerShell 7 and run Install-Module PnP.PowerShell. To update it later, run Update-Module PnP.PowerShell.

Why does Connect-PnPOnline need a client ID?

On September 9th, 2024 the shared PnP Management Shell Entra ID app was deleted. Since then, interactive sign-in works only with the client ID of an Entra ID application registered in your own tenant. Register one with Register-PnPEntraIDAppForInteractiveLogin, then pass its ID with -ClientId or store it in the ENTRAID_CLIENT_ID environment variable.

Does PnP PowerShell work with multi-factor authentication?

Yes. -Interactive and -DeviceLogin both take you through the multi-factor step. Signing in with -Credentials does not work with multi-factor authentication.

Does PnP PowerShell run on a Mac or on Linux?

Yes. The module is cross-platform and runs on Windows, Linux and macOS, and there is a Docker image, m365pnp/powershell.

Does PnP PowerShell work with SharePoint Server?

The current module supports SharePoint Online. The original Windows PowerShell cmdlets supported SharePoint 2013, 2016 and 2019 on-premises and are a separate, older module.

What is the difference between PnP PowerShell and the SharePoint Online Management Shell?

The SharePoint Online Management Shell is Microsoft's module. You connect it to the SharePoint admin center with Connect-SPOService. PnP PowerShell is the community module. You connect it to a site with Connect-PnPOnline, and it has cmdlets for lists, items, files, the recycle bin and site templates.

How do I run PnP PowerShell in a scheduled script?

Use app-only sign-in with a certificate. Register-PnPEntraIDApp creates the app registration and the certificate files, and Connect-PnPOnline takes -ClientId, -Tenant and -CertificatePath or -Thumbprint. In Azure Automation or Azure Functions, -ManagedIdentity is the alternative.

About the author
Mark Smith - Co-Founder, SmiKar Software

Mark Smith co-founded SmiKar Software in 2015 and has spent the past decade helping organisations solve Microsoft 365 data management challenges. He works with the SmiKar team to build solutions for SharePoint archiving, storage optimisation, governance and compliance, supporting customers from growing businesses through to Fortune 500 enterprises.

More about SmiKar

Ready when you are

Cut your Microsoft 365 storage bill - keep your data in your tenant.