Introduction
I use SharePoint PnP PowerShell cmdlets almost every workday to make a lot of customizations and to perform administrative tasks. I guess no introduction is needed about PnP PowerShell with its huge usage and popularity.
Now, if you are a user like me who is working on different versions of SharePoint, 2013, 2016, 2019 & Online, using the right PnP PowerShell module to target the SharePoint version is tedious.
When you install different PnP PowerShell modules (i.e., 2013, 2016, 2019 & Online), we have a cmdlet name conflict because all the modules have the same name for the cmdlets. And none of the modules are loaded initially when you open a PowerShell session.
How are the modules loaded?
When you first run any PnP PowerShell cmdlet (obviously Connect-PnPOnline), the PnP PowerShell module is automatically loaded based on the modules path. Though most of the cmdlets work the same in all versions of SharePoint, there are significant changes in how the authentication is handled and SharePoint Online always has few more cmdlets compared to the On-premise versions. So, we need to load the specific PnP Module depending on the SharePoint version we are using.
Which module (2013, 2016, 2019, Online) is loaded?
When you have different modules installed, PowerShell picks the modules to load from the default modules path (in environment variables). As this appears to be alphabetical, 2013 module is loaded by default in my case.
This blog from Christ Kent, explains how to target different versions of SharePoint PnP PowerShell by changing the PSModulePath in environment variables, and using Import-Module whenever we need to target for a different version of SharePoint. This is quick workaround is helpful, but we have to run the Import-Module cmdlet every time we need to use a different version of the PnP SharePoint module.
Another approach to do this
Isn’t it cool to have different icons like this to target the PnP PowerShell module for the specific SharePoint version? This way, you don't have to manually run any Import-Module cmdlets. These are desktop shortcuts for PowerShell specifying which module to load using the shortcut parameters. And when we open any shortcut, PowerShell is opened with the respective PnP Module already loaded.
This way, I can easily open a PowerShell session for the respective SharePoint version without the hassle of typing Import-Module cmdlets always.
How to do this?
- Type the name (target version of SharePoint) and click Finish.
Now, whenever I open PowerShell with this shortcut, I am ready to write cmdlets for SharePoint 2016. Similarly I keep 4 different shortcuts for 2013, 2016, 2019 and SharePoint Online which is handy, as I dont have to run the Import-Module cmdlet whenever I need to target a different version of SharePoint.
Summary
This shows a simple solution using the windows shortcuts and the -Command parameter to start the powershell, and to load a specific PowerShell module, enabling us to create quick shortcuts for opening the right PnP module.