oneget: error trying to install package provider (for powershell core)

I am having packaging problems with powershell core, so I submitted https://github.com/PowerShell/PowerShell/issues/10867. But @iSazonov re-directed me to oneget. I am completely lost as to what is wrong, but I thought I would let you know. Perhaps the most fundamental problem is with get-packageprovider:

PS C:\Users\e27290> gcm get-packageprovider

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Get-PackageProvider                                1.0.0.1    PackageManagement

But when I run it, I get this error:

PS C:\Users\e27290> get-packageprovider
get-packageprovider : Unhandled Exception - Message:'The type initializer for 'Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicType' threw an exception.' Name:'TypeInitializationException' Stack Trace:'   at Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicType.Create(Type tInterface, OrderedDictionary`2 instanceMethods, List`2 delegateMethods, List`1 stubMethods, List`2 usedInstances)
   at Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicInterface.CreateProxy(Type tInterface, Object[] instances)
   at Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicInterface.DynamicCast(Type tInterface, Object[] instances)
   at Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicInterface.DynamicCast[TInterface](Object[] instances)
   at Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicInterfaceExtensions.As[TInterface](Object instance)
   at Microsoft.PowerShell.PackageManagement.Cmdlets.CmdletBase.get_PackageManagementHost()
   at Microsoft.PowerShell.PackageManagement.Cmdlets.CmdletBase.SelectProviders(String[] names)
   at Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackageProvider.ProcessRecordAsync()
   at Microsoft.PowerShell.PackageManagement.Cmdlets.AsyncCmdlet.<>c__DisplayClass83_0.<AsyncRun>b__0()'
At line:1 char:1
+ get-packageprovider
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (Microsoft.PowerShel\u2026.GetPackageProvider:GetPackageProvider) [Get-PackageProvider], Exception
+ FullyQualifiedErrorId : UnhandledException,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackageProvider

This seems to be a powershell core problem not a oneget problem. (I do not have these problems with powershell.)

About this issue

Most upvoted comments

I’ve been checking in on this issue for a while and finally have a workaround that worked on my machine. Just run this in Core:

Get-ChildItem -Path $env:PSModulePath.Split(';') -Filter 'PackageManagement' -Directory -ErrorAction SilentlyContinue |
    Get-ChildItem -Directory |
    Where-Object -Property Name -Match '\d*\.\d*\.\d*' |
    Get-ChildItem -Filter 'netstandard2.0' -Directory |
    ForEach-Object -Process {
        foreach ($clr in 'fullclr', 'coreclr') {
            Copy-Item -Path $_.FullName -Destination "$($_.Parent)\$clr" -Recurse -ErrorAction SilentlyContinue
        }
    }

This lead me in the direction of fixing this problem for me in 7.2.5. $env:PSModulePath had “C:\Program Files\WindowsPowerShell\Modules” before “C:\Program Files\PowerShell\7\Modules”. When I made the v7 path the first item I was able to install modules again.

Strangely, attempting to Update-Module PackageManagement reports an issue, but otherwise I experience no issues.

PS > Update-Module PackageManagement
Update-Module: Module 'PackageManagement' was not installed by using Install-Module, so it cannot be updated.

PS Version: 7.2.2 Same problem.

This was a bug fixed in PowerShell 7.1. See https://github.com/PowerShell/PowerShell/pull/12203

I may be able to prepare for you small .NET Core (.NET 7) WinForms powered app with powershell (core) inside with 3 rows of Powershell. It is small part of bigger applications installing IIS, url rewrite etc on WinServer 2016/2019 so we need to get nuget provider there

Same problem in 7.2.3 too. I see it in both Windows powershell and powershell core.

I am trying to update DBATOOLS module and get this…

PS >update-module dbatools         
Import-PackageProvider: No match was found for the specified search criteria and provider name 'NuGet'. Try 'Get-PackageProvider -ListAvailable' to see if the provider
exists on the system.

NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available
 in 'C:\Program Files\PackageManagement\ProviderAssemblies' or 'C:\Users\chamilton\AppData\Local\PackageManagement\ProviderAssemblies'. You can   
also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to    
install and import the NuGet provider now?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y

Install-PackageProvider: Unhandled Exception - Message:'The type initializer for 'Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicType' 
threw an exception.'
Name:'TypeInitializationException' Stack Trace:'   at Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicType.Create(Type tInterface,
OrderedDictionary`2 instanceMethods, List`2 delegateMethods, List`1 stubMethods, List`2 usedInstances)    at
Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicInterface.CreateProxy(Type tInterface, Object[] instances)    at
Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicInterface.DynamicCast(Type tInterface, Object[] instances)    at
Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicInterface.DynamicCast[TInterface](Object[] instances)    at
Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicInterfaceExtensions.As[TInterface](Object instance)    at
Microsoft.PowerShell.PackageManagement.Cmdlets.CmdletBase.get_PackageManagementHost()    at
Microsoft.PowerShell.PackageManagement.Cmdlets.CmdletBase.SelectProviders(String[] names)    at
Microsoft.PowerShell.PackageManagement.Cmdlets.CmdletWithProvider.get_SelectedProviders()    at
Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackageProvider.get_SelectedProviders()    at
Microsoft.PowerShell.PackageManagement.Cmdlets.CmdletWithProvider.<get_CachedSelectedProviders>b__23_0()    at
Microsoft.PackageManagement.Internal.Utility.Extensions.DictionaryExtensions.GetOrAdd[TKey,TValue](IDictionary`2 dictionary, TKey key, Func`1      
valueFunction)    at Microsoft.PackageManagement.Internal.Utility.Extensions.Singleton`1.GetOrAdd(Func`1 newInstance, Object primaryKey, Object[]  
keys)    at Microsoft.PackageManagement.Internal.Utility.Extensions.SingletonExtensions.GetOrAdd[TResult](Object primaryKey, Func`1 newInstance,   
Object[] keys)    at Microsoft.PowerShell.PackageManagement.Cmdlets.CmdletWithProvider.get_CachedSelectedProviders()    at
Microsoft.PowerShell.PackageManagement.Cmdlets.CmdletWithProvider.GenerateDynamicParameters()    at
Microsoft.PowerShell.PackageManagement.Cmdlets.AsyncCmdlet.<>c__DisplayClass83_0.<AsyncRun>b__0()'

Import-PackageProvider: No match was found for the specified search criteria and provider name 'NuGet'. Try 'Get-PackageProvider -ListAvailable' to see if the provider
exists on the system.

Get-PackageProvider: Unhandled Exception - Message:'The type initializer for 'Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicType' threw an exception.'
Name:'TypeInitializationException' Stack Trace:'   at Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicType.Create(Type tInterface,      
OrderedDictionary`2 instanceMethods, List`2 delegateMethods, List`1 stubMethods, List`2 usedInstances)    at
Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicInterface.CreateProxy(Type tInterface, Object[] instances)    at
Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicInterface.DynamicCast(Type tInterface, Object[] instances)    at
Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicInterface.DynamicCast[TInterface](Object[] instances)    at
Microsoft.PackageManagement.Internal.Utility.Plugin.DynamicInterfaceExtensions.As[TInterface](Object instance)    at
Microsoft.PowerShell.PackageManagement.Cmdlets.CmdletBase.get_PackageManagementHost()    at
Microsoft.PowerShell.PackageManagement.Cmdlets.CmdletBase.SelectProviders(String name)    at
Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackageProvider.ProcessProvidersFilteredByName()    at
Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackageProvider.ProcessRecordAsync()    at
Microsoft.PowerShell.PackageManagement.Cmdlets.AsyncCmdlet.<>c__DisplayClass83_0.<AsyncRun>b__0()'

Update-Module: NuGet provider is required to interact with NuGet-based repositories. Please ensure that '2.8.5.201' or newer version of NuGet provider is installed.

If I run Get-PackageProvider -ListAvailable though, I see that nuget is already installed…

PS >Get-PackageProvider -ListAvailable

Name                     Version          DynamicOptions
----                     -------          --------------
nuget                    2.8.5.208

I’ve been checking in on this issue for a while and finally have a workaround that worked on my machine. Just run this in Core:

Get-ChildItem -Path $env:PSModulePath.Split(';') -Filter 'PackageManagement' -Directory -ErrorAction SilentlyContinue |
    Get-ChildItem -Directory |
    Where-Object -Property Name -Match '\d*\.\d*\.\d*' |
    Get-ChildItem -Filter 'netstandard2.0' -Directory |
    ForEach-Object -Process {
        foreach ($clr in 'fullclr', 'coreclr') {
            Copy-Item -Path $_.FullName -Destination "$($_.Parent)\$clr" -Recurse -ErrorAction SilentlyContinue
        }
    }