I'm using Windows 10 and Powershell 5.1
Get-PSRepository has result :
PSGallery Untrusted https://www.powershellgallery.com/api/v2
whereas Update-Module returns error
PackageManagement\Install-Package : Unable to find repository 'https://www.powershellgallery.com/api/v2/'. Use
Get-PSRepository to see all available repositories.
At C:\Program Files\WindowsPowerShell\Modules\powershellget\2.0.1\PSModule.psm1:13000 char:20
+ … $sid = PackageManagement\Install-Package @PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.Power….InstallPackage:InstallPackage) [Install-Package], Ex
ception
+ FullyQualifiedErrorId : SourceNotFound,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
Any idea of how to fix it?
Best Answer
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).
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:
The full run down
I have run into this incredibly annoying issue myself. From the error message we can see a couple of things:
PowerShellGet\Update-Module
ultimately passes the buck toPackageManagement\Install-Package
Running
Get-PSRepository
on my machine yields: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 aSourceLocation
that exactly matches that string? Let's try changing theSourceLocation
for PSGallery: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:
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:
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 usingInstall-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):
Running
Get-InstalledModule
again yields:Great! Now let's try removing the PSGallery1 repository and updating a module:
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.