Powershell is hell

Solving “Unable to find repository ‘https://www.powershellgallery.com/api/v2’.”

2 min read.

So, you’re running a PowerShell command that requires a connection to PowerShell gallery, but you run into an error message, somewhat like this: “Unable to find repository ‘https://www.powershellgallery.com/api/v2’. This can happen when you’re running something like Install-Module or Update-Module.

The whole error message might be somewhat like this:

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\1.0.0.1\PSModule.psm1:2089 char:20
+ ...           $sid = PackageManagement\Install-Package @PSBoundParameters
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Ex
   ception
    + FullyQualifiedErrorId : SourceNotFound,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackag

What gives?

Reason

Table of Contents

Repositories in PowerShell are a ton of fun, aren’t they? PowerShell gallery is luckily usually up, but it doesn’t always accept your TLS version (see this post for how to fix your PowerShell TLS version), but even when it does, it might still mess up.

Running Get-PSRepository revealed what’s going on:

get-psrepository

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

Did you catch it? PSGallery SourceLocation ends with a forward slash (“/”).

But PowerShell doesn’t want the trailing slash. Well, this time it doesn’t.

The reason for this is actually in a workaround I had to apply due to another issue with PSGallery back in the day – you had to have a trailing slash in the SourceLocation for it to work.

But now it’s causing issues instead. Oh, how much fun PowerShell can be, right? :)

https://www.koskila.net/how-to-fix-system-invalidoperationexception-powershell-gallery-is-currently-unavailable

Anyway, the solution is luckily quite simple. Just add the gallery without the trailing slash – because yeah, PowerShell DOES require it this time.

No worries – you can also leave the repository with the trailing slash in it. Because you might need it later.

Solution

Run this in PowerShell:

Register-PSRepository -Default
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted

If you already have a repository with the name “PSGallery”, you could register the repository with a new name – somewhat like this:

Register-PSRepository -Name PSGallery1 -SourceLocation "https://www.powershellgallery.com/api/v2" -InstallationPolicy Trusted

And with that, you should be good!

mm
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments