Home > Software design >  How to fix Nuget Provider "Find-Module" Installation error with PowerShell 7.3?
How to fix Nuget Provider "Find-Module" Installation error with PowerShell 7.3?

Time:12-03

I've been trying to run a PowerShell script, and upon doing so, I receive a message that NuGet Provider is required.

NuGet provider is required to continue
This version of PowerShellGet requires minimum version '2.8.5.201' of NuGet provider to publish an item to NuGet-based
repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\timothy.granata\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"):

If I input Y, an error is returned:

Find-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 try running Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force as it recommends, I also get an error:

Install-PackageProvider: Unable to find repository with SourceLocation ''. Use Get-PSRepository to see all available repositories.

And finally, if I run Get-PSRepository, that also errors:

Get-PackageSource: Unable to find module providers (PowerShellGet).

In the script I am trying to debug, the code that seems to trigger this prompt is Install-AWSToolsModule SecurityToken -Force. The surrounding code looks like:

if (-not (Get-Module AWS.Tools.Installer -ListAvailable)) {
    Install-Module AWS.Tools.Installer -Force
}

Install-AWSToolsModule SecurityToken -Force

Get-AWSCredential -ListProfileDetail | ForEach-Object {
    Remove-AWSCredentialProfile -ProfileName $_.ProfileName -Force
}

I have tried:

  • Reinstalling PowerShell 7
  • Making sure I am using TLS 1.2 by running [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  • Running PowerShell as Administrator
  • Deleting the Modules folder found in my C:\Users<user>\Documents\WindowsPowerShell folder

I'm unsure what else I can try at this point. How can I install the NuGet provider for use with PowerShell 7.3?

CodePudding user response:

Try

$sourceArgs = @{
    Name = 'nuget.org'
    Location = 'https://api.nuget.org/v3/index.json'
    ProviderName = 'NuGet'
}

Register-PackageSource @sourceArgs

Get-PackageProvider | where name -eq 'nuget' | Install-PackageProvider

EDIT

Perhaps try

Invoke-WebRequest 'https://www.powershellgallery.com/api/v2/package/PackageManagement/1.4.8.1' -OutFile $env:temp\nuget.zip

And confirm you're able to download the nuget package. If so, then try

Expand-Archive $env:temp\nuget.zip -DestinationPath 'C:\Program Files\PowerShell\7\Modules\PackageManagement' -Force

Import-Module PackageManagement -Verbose -Force

CodePudding user response:

It seems something with OneDrive was indeed throwing this off as I wondered in my one comment. I found this post which stated (from some Microsoft Documentation):

The user-specific CurrentUser location on Windows is the PowerShell\Modules folder located in the Documents location in your user profile ... Microsoft OneDrive can also change the location of your Documents folder.

I ran $env:PSModulePath and sure enough their was a OneDrive location. I ended up doing what the answer on that post suggested, and excluded the PowerShell directory from OneDrive. After doing this, my script seems to work now (it doesn't produce errors, or that prompt). Ever after doing this, the OneDrive location still shows up from the $env:PSModulePath command, but I guess it falls back to the next modules location if it can't find a directory.

  • Related