Fix: “The ‘Connect-PnPOnline’ command was found in the module ‘PnP.PowerShell’, but the module could not be loaded. For more information, run ‘Import-module PnP.PowerShell’.” Error.
Problem: When trying to connect to SharePoint Online from PnP PowerShell, I got this error message: “The ‘Connect-PnPOnline’ command was found in the module ‘PnP.PowerShell’, but the module could not be loaded. For more information, run ‘Import-module PnP.PowerShell’.”
Solution:
This error indicates that the ‘Connect-PnPOnline’ command is recognized as part of the ‘PnP.PowerShell’ module, but the module itself could not be loaded. To resolve this issue, follow the steps below:
- Ensure that the ‘PnP.PowerShell’ module is installed. If you haven’t installed it yet, you can do so by running the following command in an elevated PowerShell session:
Install-Module -Name PnP.PowerShell
- If the module is already installed, try importing it manually using the ‘Import-Module’ command:
Import-Module PnP.PowerShell
If you encounter any errors during the import, it will provide more information about the issue.
- Verify that you’re using a compatible version of PowerShell. The latest version of ‘PnP.PowerShell’ module requires PowerShell 7 or later. You can check your PowerShell version by running the following:
$PSVersionTable.PSVersion
If you’re using an older version of PowerShell, consider upgrading to PowerShell 7 or later.
- The error may happen when a version conflicts with the PnP PowerShell module installed in the machine. Try to uninstall and reinstall the ‘PnP.PowerShell’ module:
#Uninstall all versions of the PnP PowerShell
Uninstall-Module -Name PnP.PowerShell -AllVersions -Force
#Install pnP PowerShell module
Install-Module -Name PnP.PowerShell
- This could be a bug in the latest version of PnP PowerShell! Try uninstalling the latest version and installing the previous version.
Install-Module PnP.PowerShell -RequiredVersion 1.12 -Force
After addressing the issue, you should be able to load the ‘PnP.PowerShell’ module and use the ‘Connect-PnPOnline’ command without any problems.
I ended up using PowerShell 7 and had success with that in Admin mode. https://learn.microsoft.com/en-us/shows/it-ops-talk/how-to-install-powershell-7
Other PowerShells would not allow me to run the PnP commands for some reason.
If you’re trying to run this PnP.PowerShell in PS ISE (You probably won’t get error messages if you run PS 7), I had to do the following to make it work:
Error Message:
“File C:\temp\{your script}.ps1 cannot be loaded because running scripts is disabled on this system”
Fix: Set the execution Policy with the following command:
Set-ExecutionPolicy RemoteSigned
Type “Y” when prompted to proceed
Error Message:
“The ‘Connect-PnPOnline’ command was found in the module ‘PnP.PowerShell’, but the module could not be loaded.”
Fix: First, close PowerShell and restart and administrator.
#Uninstall all versions of the PnP PowerShell
Uninstall-Module -Name PnP.PowerShell -AllVersions -Force
Note- It should return to the command prompt with no messages. If it returns a message, restart PowerShell as Administrator.
Then
#Install PnP PowerShell module and force it to install version 1.12 (The latest version 2.1.1 didn’t work for me)
Install-Module PnP.PowerShell -RequiredVersion 1.12 -Force
Thank you Salaudeen Rajack!!
When you run the Powershell ISE or just try to run it outright from the start menu, then you need to run the “$PSVersionTable.PSVersion” command as outlined above and see that it reports version 5, then you are not running the correct Powershell EXE…
As Microsoft indicates in their link below, when we install Powershell 7, it doesn’t eliminate version 5 that is installed in base Windows. You have to actually make sure that you are running the correct EXE file.
We also have to work through the below link to get 7 properly configured on our systems.
https://learn.microsoft.com/en-us/powershell/scripting/whats-new/migrating-from-windows-powershell-51-to-powershell-7?view=powershell-7.3
I am still getting the error even with 1.12 installed. Do I need to reboot?
Actually, I found the issue altogether… You have to install Powershell 7 now…
Same issue for myself