How to use PnP PowerShell in SharePoint Online/2013/2016/2019

Hello Folks, This piece of the article talks about getting started with PnP PowerShell which applies to SharePoint 2013/2016 as well as SharePoint Online to manage the SharePoint environment remotely via PowerShell Scripting as the commands use CSOM.

If you take PowerShell which means in simple terms you are going to automate some repetitive admin-related tasks which help to ease the job of developers/admins drastically.

sharepoint pnp powershell
sharepoint pnp powershell

To get started, the first thing is to set up the environment. There are certain steps to be followed for the setup.

  • Installation

There are 2 ways to install the cmdlets. We recommend, where possible, installing them from the PowerShell Gallery. Check out the “Getting Started with the Gallery” section to make sure you have all requirements in place.

Alternatively, you can download the setup files and install the cmdlets directly.

  • PowerShell Gallery

If your main OS is Windows 10, or if you have PowerShellGet installed, you can run the following commands to install the PowerShell cmdlets:

SharePoint VersionCommand to install
SharePoint OnlineInstall-Module SharePointPnPPowerShellOnline
SharePoint 2016Install-Module SharePointPnPPowerShell2016
SharePoint 2013Install-Module SharePointPnPPowerShell2013

If your System is running with PowerShell Version 3 or 4, you’ll get an error stating below when you try executing the SharePoint Online Command,

Run this command to check the version of your PowerShell installed in your system

$PSVersionTable.PSVersion

PnP PowerShell SharePoint
PnP PowerShell SharePoint

To get that sorted out, you’ll need to install the PackageManagement PowerShell Modules Preview – March 2016 to proceed forward

The preview release includes PackageManagement (previously known as OneGet) and PowerShellGet Windows PowerShell modules. The versions of these modules include the functionality of those that shipped with WMF 5.0.

The preview also installs exclusively on systems running Windows PowerShell 4 or Windows PowerShell 3 only. This preview will not install on any systems running Windows PowerShell 5 (including Windows 10) which already ship with these modules.

If you plan to move to Windows PowerShell 5, we advise you to skip installing this preview and instead install our WMF 5.0 or upgrade to Windows 10.

After successful installation of PackageManagement PowerShell Modules Preview – March 2016 in the server, proceed forward

For this demo, I’ll be using Windows 10 as base OS. Open PowerShell ISE from your Windows 10 machine

sharepoint pnp powershell download
sharepoint pnp powershell download

Execute this command for SharePoint Online in the PowerShell Editor

Install-Module SharePointPnPPowerShellOnline

It gives a popup stating PowerShellGet Requires NuGet Provider version ‘2.8.5.201’ or newer to interact with NuGet-based repositories.

install pnp powershell for sharepoint online
PnP PowerShell

Click “Yes” to install the NuGetProvider Packet Management

PowerShell will download the DLL from the oneget.org site

During the process, you get a popup stating that you are installing the modules from an untrusted repository

sharepoint online pnp powershell
sharepoint online pnp powershell

Click “Yes” to proceed.

The SharePointPnPPowerShellOnline package will start getting installed

install pnp powershell for sharepoint online
sharepoint pnp powershell

After successful installation, verify the installed module by running this command

Get-Module SharePointPnPPowerShell* -ListAvailable | Select-Object Name,Version | Sort-Object Version -Descending

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

Name                                                                     Version

—-                                                                            ——-

SharePointPnPPowerShellOnline       3.2.1810.0

Here is how you can connect to a SharePoint Online Site using PnP PowerShell

Connect-PnPOnline -Url https://o365cr7.sharepoint.com -Credentials (Get-Credential)

And enter the credentials to connect to the site

pnp powershell for sharepoint online
PnP PowerShell SharePoint 2016

Once you are connected to the SharePoint online site you can perform numerous operations like Create, Read, Update, Delete operations, and a lot more. To see the available cmdlets for the installed PnP module, execute this command,

Get-Command -Module *PnP*

PS C:\WINDOWS\system32> Get-Command -Module *PnP*

CommandTypeNameVersionSource
AliasConnect-PnPHubSite3.2.1810.0SharePointPnPPowerShellOnline
AliasDisconnect-PnPHubSite3.2.1810.0SharePointPnPPowerShellOnline
AliasEnsure-PnPFolder3.2.1810.0SharePointPnPPowerShellOnline
AliasExecute-PnPQuery3.2.1810.0SharePointPnPPowerShellOnline
AliasLoad-PnPProvisioningTemplate3.2.1810.0SharePointPnPPowerShellOnline
FunctionDisable-PnpDevice1.0.0.0PnpDevice
FunctionEnable-PnpDevice1.0.0.0PnpDevice
FunctionGet-PnpDevice1.0.0.0PnpDevice
FunctionGet-PnpDeviceProperty1.0.0.0PnpDevice
CmdletAdd-PnPApp3.2.1810.0SharePointPnPPowerShellOnline
CmdletAdd-PnPClientSidePage3.2.1810.0SharePointPnPPowerShellOnline
CmdletAdd-PnPClientSidePageSection3.2.1810.0SharePointPnPPowerShellOnline
CmdletAdd-PnPClientSideText3.2.1810.0SharePointPnPPowerShellOnline
CmdletAdd-PnPClientSideWebPart3.2.1810.0SharePointPnPPowerShellOnline

Hope this article will be helpful to get started with PnP PowerShell in SharePoint Online or SharePoint 2016/2013.

You may like the following PowerShell tutorials:

  • >