Unable to find repository on Update-Module

TL;DR

It looks like the URL for the PSGallery repository registered in PowerShell used to point to https://www.powershellgallery.com/api/v2/ but it was changed to https://www.powershellgallery.com/api/v2 at some point (note the missing forward slash at the end).

λ  Get-PSRepository

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Untrusted            https://www.powershellgallery.com/api/v2

Any modules installed from the old URL are now failing to update. Reinstalling them from PowerShell gallery will update the repository URL, allowing the modules to be updated normally going forward. You can use the following command to reinstall all modules pointing to the old URL:

Get-InstalledModule `
| ? { $_.Repository -eq 'https://www.powershellgallery.com/api/v2/' } `
| % { Install-Package -Name $_.Name -Source PSGallery -Force -AcceptLicense }

The full run down

I have run into this incredibly annoying issue myself. From the error message we can see a couple of things:

PackageManagement\Install-Package : Unable to find repository ‘https://www.powershellgallery.com/api/v2/’

  1. PowerShellGet\Update-Module ultimately passes the buck to
    PackageManagement\Install-Package
  2. It is looking for a repository
    at ‘https://www.powershellgallery.com/api/v2/’

Running Get-PSRepository on my machine yields:

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Trusted              https://www.powershellgallery.com/api/v2

So it looks like the repository is there, except, maybe it isn’t. Take note of the trailing forward slash. Could it be that Install-Package is looking for a repository with a SourceLocation that exactly matches that string? Let’s try changing the SourceLocation for PSGallery:

Set-PSRepository -Name PSGallery -SourceLocation https://www.powershellgallery.com/api/v2/ -InstallationPolicy Trusted

PackageManagement\Set-PackageSource : The PSGallery repository has
pre-defined locations. The ‘Location, NewLocation or SourceLocation’
parameter is not allowed, try again after removing the ‘Location,
NewLocation or SourceLocation’ parameter. At C:\Program
Files\WindowsPowerShell\Modules\PowerShellGet\2.0.4\PSModule.psm1:11768
char:17
+ … $null = PackageManagement\Set-PackageSource @PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (https://www.pow…ery.com/api/v2/:String) [Set-PackageSource],
Exception
+ FullyQualifiedErrorId : ParameterIsNotAllowedWithPSGallery,Add-PackageSource,Microsoft.PowerShell.PackageManagement.Cmdlets.SetPackageSource

Well, that didn’t work. Looks like the PSGallery repository is protected for your safety.

Let’s trying adding a different repository and updating a module:

Register-PSRepository -Name PSGallery1 -SourceLocation https://www.powershellgallery.com/api/v2/ -InstallationPolicy Trusted
Update-Module -Name pester -Force

Look, no error. It works!

Here is the interesting thing, if I pull up a list of installed modules I find a mix of repositories:

Get-InstalledModule | Select Name, Repository | FT -AutoSize

Name                         Repository
----                         ----------
7Zip4Powershell              PSGallery 
AWSPowerShell                PSGallery 
cChoco                       PSGallery1
dbatools                     PSGallery 
DLMAutomation                PSGallery1
InvokeBuild                  PSGallery1
Microsoft.PowerShell.Archive PSGallery1
PackageManagement            PSGallery 
Pester                       PSGallery1
posh-git                     PSGallery1
powershell-yaml              PSGallery1
PowerShellGet                PSGallery 
PowerUpSQL                   PSGallery1
psake                        PSGallery1
PsHosts                      PSGallery1
psTrustedHosts               PSGallery1
ReverseDSC                   PSGallery1
SeeShell                     PSGallery1
SqlServer                    PSGallery1
TunableSSLValidator          PSGallery1
xSmbShare                    PSGallery1
xWebAdministration           PSGallery1

Look at all those modules installed form PSGallery1 which is associated with https://www.powershellgallery.com/api/v2/! Prior to just now, there has never been a repository on my machine called PSGallery1; every module I have ever installed has been from PSGallery. My guess is that the PSGallery repository used to point to https://www.powershellgallery.com/api/v2/ and at some point, intentionally or not, it was changed to https://www.powershellgallery.com/avp/v2; breaking Update-Module for any modules installed from the previous URL. I suspect that if I reinstall the modules using Install-Package from the updated PSGallery repository everything will resolve itself and I can remove the PSGallery1 repository.

Let’s update all the modules that were deployed from the old URL (PSGallery1):

Get-InstalledModule `
| ? { $_.Repository -eq 'PSGallery1' } `
| % { Install-Package -Name $_.Name -Source PSGallery -Force -AcceptLicense }

Running Get-InstalledModule again yields:

Name                         Repository
----                         ----------
7Zip4Powershell              PSGallery
AWSPowerShell                PSGallery
cChoco                       PSGallery
dbatools                     PSGallery
DLMAutomation                PSGallery
InvokeBuild                  PSGallery
Microsoft.PowerShell.Archive PSGallery
PackageManagement            PSGallery
Pester                       PSGallery
posh-git                     PSGallery
powershell-yaml              PSGallery
PowerShellGet                PSGallery
PowerUpSQL                   PSGallery
psake                        PSGallery
PsHosts                      PSGallery
psTrustedHosts               PSGallery
ReverseDSC                   PSGallery
SeeShell                     PSGallery
SqlServer                    PSGallery
TunableSSLValidator          PSGallery
xSmbShare                    PSGallery
xWebAdministration           PSGallery

Great! Now let’s try removing the PSGallery1 repository and updating a module:

Unregister-PSRepository PSGallery1
Update-Module -Name pester -Force

Success! The module updated without error.

I’m not sure what is broken here, the URL for the PSGallery repository or Install-Package, but reinstalling all modules that were installed from the old URL seems to fix everything.

Leave a Comment