An Approach to Manage the Versions of PnP PowerShell Module

If your are a developer who uses PnP PowerShell module to deploy and configure your environment or your clients’, you have certainly faced some issues to manage the versions of PnP PowerShell module with your scripts.

I will share with you an approach that I am using myself to use my current scripts and the old ones easier while dealing with the appropriate PnP PowerShell version.

Install module

This first step is downloading a version of PnP PowerShell Module in accordance with your environment from the official GitHub repository:

  • SharePointPnPPowerShell2013.msi: for SharePoint 2013 On-Premise only
  • SharePointPnPPowerShell2016.msi: for SharePoint 2016 On-Premise only
  • SharePointPnPPowerShell2019.msi: for SharePoint 2016 On-Premise only
  • SharePointPnPPowerShellOnline.msi: for SharePoint Online only

Once downloaded, you can install it on your computer:

  1. Launch the executable (.msi file)
  1. Accept the terms in the Licence Agreement and click on Advanced
    LsOnline-PnPVersionMngt-install-step01
  2. Choose Install for all users of this machine (it is easier to retrieve the path of files)
    LsOnline-PnPVersionMngt-msi-step02
  3. If necessary, you can change the path of the PowerShell module, but I suggest you to keep the default path. Copy this one for later
    LsOnline-PnPVersionMngt-msi-step03
  4. Now, click on Install then Finish

That’s it! The PnP PowerShell module is now installed and available from a Windows PowerShell.

If you wish to change the version, you have to remove the current version and then download and install a new one. At this time, you can meet two issues:

  1. your version is not the latest one and you want to run newest scripts that use some cmdlets that do not exist with your version – you have to uninstall the current version and install the latest one
  2. your version is the latest one and you want to run oldest scripts – you can have some errors because some cmdlets do not exist anymore – you have to uninstall the current version and install the previous one in accordance with your script

In both cases, you then have to either uninstall the current version and use the same than previously to keep working, or update your scripts to work with this other version.

Manage version

Rather than uninstall and install an other version of the PnP PowerShell module each time you need to change the version, you can keep each library locally and use the good one “On Demand”.

In the previous chapter, the PnP PowerShell module setup has installed the library to C:\Program Files (x86)\SharePointPnPPowerShellOnline\ by default.

An approach to manage the versions of PnP PowerShell module
Default location of PnP PowerShell library files

The idea is to copy all of the files from this folder to a new one.

To manage the versions easier, create a folder with this pattern for each version [YYYY][MM][-XX].

Examples:

  • 202002: module released the February 2020
  • 202001-01: module released the January 2020. This is an intermediate release, this is why I added -01. Increase this number for each intermediate release in the same month of the year
  • 202001: module released the January 2020

To access easily these folders, keep an easy path, without special char, no space, like:

C:\PnP\SharePointPnPPowerShellOnline

Here, an example of my “versioning” folders:

An approach to manage the versions of PnP PowerShell module
Screenshot of my PnP PowerShell folders per version

Use specific version in your PowerShell session

Now that you have stored each version that you need, how could you use them?

Before trying to execute some PnP cmdlets from PowerShell ISE or a Windows PowerShell, you have to add manually the module like this:

$dllPath = "C:\PnP\SharePointPnPPowerShellOnline\202001"

$dllClientPath365 = Join-Path $dllPath "Microsoft.SharePoint.Client.dll"
$dllRuntimePath365 = Join-Path $dllPath "Microsoft.SharePoint.Client.Runtime.dll"
$dllConnectPath365 = Join-Path $dllPath "SharePointPnP.PowerShell.Online.Commands.dll"

Import-Module $dllConnectPath365

Add-Type -Path $dllClientPath365
Add-Type -Path $dllRuntimePath365

Copy

⚠️ Important: it is not recommended to run a PnP PowerShell script with two or more versions of the same module. That can generate some errors. Here an example after importing the module while an other version is installed on the computer:

An approach to manage the versions of PnP PowerShell module
PnP Connect cmdlet result from ISE when several versions are available

To avoid this kind of annoyance, please ensure you have uninstalled the version on your computer via the installer. To remove the version installed on your computer, follow the below steps or use the “Add or remove programs“:

  1. Launch the executable (.msi file)
  1. On the select operation section, choose Remove
    LsOnline-PnPVersionMngt-msi-Remove

At this point, you can execute PnP PowerShell cmdlets. If you want to switch the module version, close the current Windows PowerShell and reuse the code above by changing the $dllPath variable:

$dllPath = "C:\PnP\SharePointPnPPowerShellOnline\202003"

CopyChange PnP library path to use the March release version

Embed module into your packages

In my case, because I have to use my scripts for several of my clients, that could get hard to ensure the prerequisites because:

  • some of them can not install anything on their computer
  • the installed version is the good one
  • for some users, go to GitHub and get the good release, install it, etc. it is not easy

When I am preparing a package, I usually use this nomenclature of folders:

  • [solutionName]\Setup.ps1: main PowerShell script that uses PnP PowerShell module
  • [solutionName]\references: folder that contains the library files
  • [solutionName]\other-folder: as many folders as you need

In my setup scripts, I systematically include this code at the top:

$global:CommandDirectory = Split-Path $SCRIPT:MyInvocation.MyCommand.Path -Parent

Push-Location $CommandDirectory

$dllClientPath365 = Join-Path -Path $global:CommandDirectory -ChildPath "references\Microsoft.SharePoint.Client.dll"
$dllRuntimePath365 = Join-Path -Path $global:CommandDirectory -ChildPath "references\Microsoft.SharePoint.Client.Runtime.dll"
$dllConnectPath365 = Join-Path -Path $global:CommandDirectory -ChildPath "references\SharePointPnP.PowerShell.Online.Commands.dll"

Import-Module $dllConnectPath365

Add-Type -Path $dllClientPath365
Add-Type -Path $dllRuntimePath365

Copy

Now, you can share the [solutionName] folder and everyone can execute the script without installing the PnP PowerShell previously.

Hoping this post will help you 😉

For more news check out our resource centre

About the Author:

Microsoft 365 Architect / Consultant – Learning and sharing about my interests. SharePoint, Microsoft365, Modern Workplace

Reference:

Sittler, L. (2020). An approach to manage the versions of PnP PowerShell module. Available at: https://blog.lsonline.fr/2020/03/14/an-approach-to-manage-the-versions-of-pnp-powershell-module/ [Accessed: 26th September 2020].

Share this on...

Rate this Post:

Share:

Topics:

PowerShell

Tags: