Import-Module PnP.PowerShell Error - Could not load file or assembly

Recently I was working on custom development. I needed to run a PowerShell Command, and when I tried to run that command, I didn’t find basic commands like Register-PnPManagementShellAccess” or “Connect-PnPOnline” commands. So, I uninstalled everything and reinstalled it. But still, I was getting below error.

Error

Import-Module: Could not load file or assembly 'System.Management.Automation, Version=7.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

Import Module

Then I thought it might be helpful if I switched to Windows PowerShell ISE to debug what was happening. And it showed me more details, like the commands I was searching for were not included in the module.

Windows Powershell

Solution

After some digging, I found out that this issue happened with the newer version of PnP PowerShell 2.1.1. We need to forcefully install the 1.12.0 version.

Uninstall all versions and then install the correct 1.12.0 version. Use the below command to remove all the versions.

Uninstall-Module PNP.PowerShell -AllVersions

Set the Tls12 protocol with the below command.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Install NuGet using the below command.

Install-PackageProvider -Name nuget -MinimumVersion 2.8.5.201 -force

Install PnP.PowerShell with version 1.12.0 using the below command.

Install-Module -Name "PnP.PowerShell" -RequiredVersion 1.12.0 -Force –AllowClobber

Problem solved

Reference