Import-Module : Could not load file or assembly 'System.Management.Automation

H Risbud 251 Reputation points
2023-04-04T17:22:30.65+00:00

I'm unable to run PnP PowerShell commands using PowerShell ISE in administrative mode. Getting following error

PS C:\WINDOWS\system32> Import-Module PnP.PowerShell
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.
At line:1 char:1
+ Import-Module PnP.PowerShell
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.ImportModuleCommand

My PowerShell ISE details PS C:\WINDOWS\system32> $PSVersionTable Name Value ---- ----- PSVersion 5.1.19041.1 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.19041.1 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 PS C:\WINDOWS\system32> get-host|Select-Object Name : Windows PowerShell ISE Host Version : 5.1.19041.1 InstanceId : d91d81d9-be20-492f-be62-f5ea45de0765 UI : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture : en-US CurrentUICulture : en-US PrivateData : Microsoft.PowerShell.Host.ISE.ISEOptions DebuggerEnabled : True IsRunspacePushed : FalseRunspace : System.Management.Automation.Runspaces.LocalRunspace Please help me to solve this error. Thanks in advance

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,628 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,059 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 31,076 Reputation points Microsoft Vendor
    2023-04-05T02:35:54.4933333+00:00

    Hi @H Risbud ,

    I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    [Import-Module : Could not load file or assembly 'System.Management.Automation] Issue Symptom: unable to run PnP PowerShell commands using PowerShell ISE in administrative mode. Getting following 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.
    

    Solution:

    1. Uninstall all versions and then install correct 1.12.0 version. Newer version PnP PowerShell 2.1.1 has issues #1. This issue happened with newer version PnP PowerShell 2.1.1.
    2. Set Tls12 protocol [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    3. Install NuGet Install-PackageProvider -Name nuget -MinimumVersion 2.8.5.201 -force
    4. Install PnP.PowerShell with version 1.12.0 Install-Module -Name "PnP.PowerShell" -RequiredVersion 1.12.0 -Force -AllowClobber

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!

    41 people found this answer helpful.

7 additional answers

Sort by: Most helpful
  1. H Risbud 251 Reputation points
    2023-04-04T22:16:53.0066667+00:00

    Solved with this process - Uninstall all versions and then install correct 1.12.0 version. Newer version PnP PowerShell 2.1.1 has issues #1. This issue happened with newer version PnP PowerShell 2.1.1. So uninstall all PnP.Powershell versions. No need to reboot machine Uninstall-Module PnP.PowerShell -AllVersions #2. Set Tls12 protocol [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #this setting already present [Net.ServicePointManager]::SecurityProtocol #3. Install NuGet Install-PackageProvider -Name nuget -MinimumVersion 2.8.5.201 -force #4. Install PnP.PowerShell with version 1.12.0 Install-Module -Name "PnP.PowerShell" -RequiredVersion 1.12.0 -Force -AllowClobber

    20 people found this answer helpful.

  2. Tony M 70 Reputation points
    2023-05-11T14:43:24.24+00:00

    So I felt the need to come in here and provide some extra information as I think the BIGGER problem is not visible for everyone here.

    Installing the Older version of PNP.Powershell is actually the wrong direction as I found out yesterday. It boils down to the Powershell version you are running on your computer.

    2023-05-11 10_09_38-Installing PnP PowerShell _ PnP PowerShell — Mozilla Firefox

    With this important point noted, here is some extra guidance.

    https://learn.microsoft.com/en-us/powershell/scripting/whats-new/migrating-from-windows-powershell-51-to-powershell-7?view=powershell-7.3

    When reading the above link from Microsoft, they correctly tell us that Powershell 7 must be installed and it will NOT overwrite Powershell 5. The two versions run side-by-side on your computer. When you open your Powershell command, be sure to run the "$PSVersionTable.PSVersion" to see what version you are running as the EXEs are NOT the same! You have to look for the new installation in your Start menu to run the correct version or go directly to the install path to run the correct EXE.

    Now what about the BIG question... Those of us who have been developing scripts for making our lives easier and might have been using the ISE. Yup, the ISE does not run version 7. We must go install https://code.visualstudio.com/ in order to get to that same usage. (This is covered in the above link as well.)

    I hope all of this helps!

    P.S. Thank you Microsoft for making things complicated... 😂😂😂

    14 people found this answer helpful.

  3. Pérez Benito, Javier 25 Reputation points
    2023-05-22T13:31:16.1733333+00:00

    It was so simple my problem.... Well I used scripts that call powershell commands using bypass, so, my error was that there I was calling with the command "powershell" at the start of the line when now with Powershell 7 has to be "pwsh". Changing this my scripts doesn't show up more problems and it work as expected. I guess it would be nice that through open Powershell 7 the scripts could use the old poweshell call... But for some reason you have to rewrite your own script in order to use now Powershell 7.

    3 people found this answer helpful.
    0 comments No comments

  4. Pérez Benito, Javier 25 Reputation points
    2023-05-11T09:36:10.86+00:00

    This is not a solution, IT A MESS. New installation of Windows 11 22H2 open powershell as administrator and install the module pnp.powershell, after that IT DOESNT WORK ANYTHING THAT I TRIED.

    First WHY?

    Error: No se puede cargar el archivo o ensamblado 'System.Management.Automation, Version=7.2.0.0,

    Connect-PnPOnline : El comando 'Connect-PnPOnline' se encontró en el módulo 'PnP.PowerShell', pero el módulo no pudo cargarse. Para obtener más información, ejecute 'Import-Module PnP.PowerShell'.

    Disconnect-PnPOnline : El comando 'Disconnect-PnPOnline' se encontró en el módulo 'PnP.PowerShell', pero el módulo no pudo cargarse. Para obtener más información, ejecute 'Import-Module PnP.PowerShell'.

    Any command that is include on PnP-Powershell it give me the same result. All of them.

    Try to remove a new version to put an old one It doesn't solve the problem with the actual version it just and crosse your finger and wait... Where is the notification on any site of this issue?

    Even so any time that I try to remove the module I had to restart the computer seems pnp-powershell module it tell me always that is on use.

    It was working fine a few weeks ago...

    2 people found this answer helpful.