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’.”

The 'Connect-PnPOnline' command was found in the module 'PnP.PowerShell', but the module could not be loaded

Solutions:

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:

1. Install PnP.PowerShell Module

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

2. Import the PnP.PowerShell Module

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.

3. Check your PowerShell Version

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.

4. Try Uninstall-Reinstall method

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

5. PowerShell ISE doesn’t support the latest PnP.PowerShell Module

Use Visual Studio Code instead of PowerShell ISE – As PowerShell ISE works only for PowerShell 5.x. The PnP.PowerShell’s latest module is built on PowerShell 7x. If you have to use PnP PowerShell in PowerShell ISE, use its classic version! How to Connect to SharePoint Online using PnP PowerShell?

Connect-PnPOnline The 'Connect-PnPOnline' command was found in the module 'PnP.PowerShell', but the module

You can install the previous version with:

Install-Module PnP.PowerShell -RequiredVersion 1.12 -Force

6. Check your PowerShell execution Policy

Last but not least: Check the PowerShell execution policy on your machine. Set it to “RemoteSigned” at least.

After addressing the issue, you should be able to load the ‘PnP.PowerShell’ module and use the ‘Connect-PnPOnline’ command without any problems. The above steps are applicable to any other PowerShell modules. E.g. “The ‘Connect-MgGraph’ command was found in the module ‘Microsoft.Graph.Authentication’, but the
module could not be loaded.”

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

7 thoughts on “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.

  • 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.

    Reply
  • 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

    Reply
  • Thank you Salaudeen Rajack!!

    Reply
  • 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

    Reply
  • I am still getting the error even with 1.12 installed. Do I need to reboot?

    Reply
    • Actually, I found the issue altogether… You have to install Powershell 7 now…

      Reply
    • Same issue for myself

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *