PnP PowerShell and more...

Here I occasionally post about Microsoft 365 Patterns and Practices in general and PnP PowerShell more specifically.

Running the various versions of PnP PowerShell side-by-side

2018-03-26 3 min read

You might be aware of the fact that we currently release 3 versions of PnP PowerShell:

Now what if you want all these versions installed, because you maybe target different versions of SharePoint?

Why 3 versions?

Let me first explain why we have 3 versions. It has to do with the underlying libraries we are using. PnP PowerShell makes use of the PnP Core Library. And that library is in its turn using the SharePoint Client-Side Object Model Libraries (CSOM). And those libraries come specifically for each version of SharePoint. As each version of SharePoint is different, the CSOM libraries reflect those differences. That means that some functionality that is available for SharePoint Online (which is the most capable on when it comes to CSOM) is not available for SharePoint 2013. And as a result, just like CSOM, we have 3 versions of the PnP Core Library and 3 version of PnP PowerShell. (And soon 4 versions, when SharePoint 2019 is released…)

Run them side by side

Out of the box this won’t work. Well, you can install them, but PowerShell will automatically load the first one it finds. So in order to use them we will have to do some tweaking.

Make sure that you have uninstalled all instances of PnP Powershell that you currently have available on your machine. If you installed it previously using install-module, simply use uninstall-module -Name SharePointPnPPowerShell<insertyourversionhere> -AllVersions.

Now download all of the modules to a location that you are aware off, say you created a folder called c:\bin\pnppowershell Use the following commands to download them:

Save-Module -Name SharePointPnPPowerShell2013 -Path c:\bin\pnppowershell
Save-Module -Name SharePointPnPPowerShell2016 -Path c:\bin\pnppowershell
Save-Module -Name SharePointPnPPowerShellOnline -Path c:\bin\pnppowershell

From now on, if you want to use a specific version of PnP PowerShell, you will have to manually import them (make sure to replace the version number with your current version):

import-module C:\bin\pnppowershell\SharePointPnPPowerShell2013\2.24.1803.0\sharepointpnppowershell2013.psd1 -DisableNameChecking

Notice that I specified the -DisableNameChecking parameter. While it just works fine without this parameter, you will receive a warning that there are cmdlets in the module that do not use approved verbs. (Apply-PnPProvisioningTemplate is one of those, as Apply- is a not an approved verb). You can safely ignore this warning.

You can of course put this in a script in your path, so it’s easier to load the various modules. Keep in mind though that you cannot have multiple PnP Modules loaded at the same time in the same PowerShell session (PowerShell does not allow you to unload a module). So if you want to switch to another version you will have to restart PowerShell.

Running against multiple version of SharePoint in one script

There are basically two ways of approaching that.

Pick the version of PnP PowerShell that supports the oldest version of SharePoint you are working with.

So if you are working against SharePoint 2013 and SharePoint Online, then use the PnP PowerShell module for SharePoint 2013. Most cmdlets will actually work as is towards SharePoint Online, but you will lack things like handling modern sites, creating modern pages etc.

Build a somewhat more advanced script

Create a script that launches new instances of PowerShell, and then launces a script in that new instance. In that launched script you load the version of PnP PowerShell that you require.

script1.ps

import-module C:\bin\pnppowershell\SharePointPnPPowerShell2013\2.24.1803.0\sharepointpnppowershell2013.psd1 -DisableNameChecking
# do your SharePoint 2013 things here
Connect-PnPOnline -Url https://myserver -CurrentCredentials
Get-PnPList

# here we launch the script to do something with SharePoint Online
PowerShell .\script2.ps1

script2.ps1

import-module C:\bin\pnppowershell\SharePointPnPPowerShell2013\2.24.1803.0\sharepointpnppowershell2013.psd1 -DisableNameChecking
Connect-PnPOnline -Url https://contoso.sharepoint.com
Get-PnPList