Powershell fails to Import-Module Microsoft.PowerShell.LocalAccounts

Konstantyn Aleksandrov 1 Reputation point

I have a simple PowerShell script that uses the Get-LocalGroupMemeber cmdlet, which is part of the LocalAccounts module.

The problem is that sometimes my script fails because the Microsoft.PowerShell.LocalAccounts module couldn't be loaded. The worst part is that the problem appears only a few times out of dozens. Meaning, if I re-run the script right after its failure it finishes successfully.

Here is the stack trace of the error:

The 'Get-LocalGroupMember' command was found in the module 'Microsoft.PowerShell.LocalAccounts', but the module could not be loaded. 
For more information, run 'Import-Module Microsoft.PowerShell.LocalAccounts'. 
---$gt; System.Management.Automation.CmdletInvocationException: Could not load file or assembly 'file:///C:\windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.LocalAccounts\\Microsoft.Powershell.LocalAccounts.dll' or one of its dependencies. 
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) ---$gt; System.IO.FileLoadException: Could not load file or assembly 'file:///C:\windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.LocalAccounts\\Microsoft.Powershell.LocalAccounts.dll' or one of its dependencies. 
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) ---$gt; System.Runtime.InteropServices.COMException: 
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

Environment info:
PS C:\Users\test> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17763.1971
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.1971
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3

PS C:\Users\test> Get-ComputerInfo

WindowsBuildLabEx                                       : 17763.1.amd64fre.rs5_release.180914-1434
WindowsCurrentVersion                                   : 6.3
WindowsEditionId                                        : EnterpriseS
WindowsInstallationType                                 : Client
WindowsProductName                                      : Windows 10 Enterprise LTSC 2019
WindowsVersion                                          : 1809
OsArchitecture                                          : 64-bit

Below is the code snippet where the actual failure occurs. Nothing fancy..

        if (!(Get-Module -Name 'Microsoft.PowerShell.LocalAccounts')) {
            Write-Verbose 'Import Microsoft.PowerShell.LocalAccounts module'
            Import-Module -Name 'Microsoft.PowerShell.LocalAccounts' -Verbose
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,435 questions
0 comments No comments
{count} votes