SharePoint / Office 365 Dev Patterns and Practices (PnP) Common PowerShell Command Reference

The SharePoint / Office 365 Developer Patterns and Practices (PnP) initiative provides a massive array of guidance, code samples and most often used by me, a PnP PowerShell command set that makes working with SharePoint (Online and on-prem) that much easier. General documentation and samples are available direct from GitHub and elsewhere, but I found I need to keep providing the same command references again and again. Therefore I provide the following list of commands and samples as a reference of what I find I use most commonly.

Prerequisites

First of course always ensure that you have the SharePoint PnP PowerShell commands installed. You can follow the installation instructions from the SharePoint PnP PowerShell GitHub repo.

Import PnP PowerShell module if it isn’t already loaded

1
C:\> Import-Module Microsoft.Online.Sharepoint.PowerShell

Check to see if PnP PowerShell is installed, and review available PnP commands

1
C:\> Get-Command -Module *PnP*

pnp-command-get-command

Or just get current PnP PowerShell version(s) installed

1
C:\> Get-Module SharePointPnPPowerShell* -ListAvailable | Select-Object Name,Version | Sort-Object Version -Descending

Update PnP PowerShell

1
C:\> Update-Module SharePointPnPPowerShell*

Uninstall a specific version of PnP PowerShell

1
C:\> Get-InstalledModule -Name "SharePointPnPPowerShellOnline" -RequiredVersion 2.25.1804.1 | Uninstall-Module

Please the “version” above, i.e. 2.25.1804.1, with the version you wish to remove. Remember to run PowerShell as an Administrator, also you will want to ensure that no other shell has loaded the module you are looking to uninstall.

Get credentials (i.e. username and password) and save this in a varialbe for repeated use

Connecting to SharePoint is simple enough, but often I prefer to get and temporarily store my login credentials for reuse, i.e. during script debugging.

1
C:\> $credential = Get-credential

pnp-command-get-credential

Connect to a SharePoint site using stored credentials

In the following example, I am asked to provide credentials and then using these credentials (stored in the variable, “$credential”), PowerShell will connect to my sample site in question, http://pixelmill-intranet/.

1
2
C:\> $credential = Get-credential
C:\> Connect-PnPOnline -url http://pixelmill-intranet/ -Credential $credential

Enable a trace log for debugging

Want to debug a script? no problem, set the SPOTraceLog

1
C:\> Set-PnPTraceLog -On -Level Debug

Get PowerShell Command help

Each PnP commandlet includes rather detailed help. Use “Get-Help” with the “-Detailed” parameter to get more information.

1
C:\> Get-Help Connect-PnPOnline -Detailed

Export a provisioning template

One of my favorite reasons to use PnP PowerShell is for site and site asset provisioning. You can also export a provisioning template from an existing SharePoint site.

In the following example, I am exporting a provisioning template from an example SharePoint Online site. You will want to change three primary variables to enable this to work for you. You could create a PS script with parameters based on the sample below.

$tenant: Set this to your specific SharePoint Online tenant sub domain. In this example, my tenant URL is: https://MOD513885.sharepoint.com, thus $tenant will be set the value of “MOD513885”.

$sourceSite: The path to the site to export. I am going to export from a marketing site I created, thus “/sites/marketing”.

$path: The path on your workstation where you want to save the exported provisioning template.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$credential = Get-credential

# Define variables
$tenant = "MOD513885";
$sourceSite = "/sites/marketing";
$path = "C:\provision-test\MyPnPTemplate.xml";

# Get context to the site in question
$webUrl = "https://{0}.sharepoint.com{1}/" -f $tenant, $sourceSite;
Write-Output $("Connecting to {0}..." -f $webUrl);
Connect-PnPOnline -Url $webUrl -Credentials $credential;
Write-Output "Context obtained";

# Extract template
Write-Output "Creating PnP template...";
Get-PnPProvisioningTemplate -Out $path;
Write-Output $("Template saved to {0}" –f $path);

Apply a provisioning Template

My go to source for applying a provisioning template is straight to the PnP PowerShell GitHub documentation.
https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/ApplyPnPProvisioningTemplate.md

1
Apply-PnPProvisioningTemplate

Provisioning template valid handlers

Provisioning templates may have a set of handlers that may then be selectively applied to a particular site during provisioning. The best list of handlers I have found is directly in the source code for the Handler model.

https://github.com/SharePoint/PnP-Sites-Core/blob/master/Core/OfficeDevPnP.Core/Framework/Provisioning/Model/Handlers.cs

An evergreen post

As I continue to find more common threads in how I work with PnP PowerShell and what questions I continue to receive, I will update this post to keep it current.

Speak Your Mind

*