Powershell is hell

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

This post was most recently updated on September 12th, 2021.

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.

This pretty much blocks you from doing anything that would require the use of new modules – so, quite a lot. Worth fixing, then. 😁

Problem

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

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? :)

The earlier article is linked below:

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

Time needed: 5 minutes

How to fix “Unable to find repository https://www.powershellgallery.com/api/v2” in PowerShell?

  1. Open PowerShell as an admin

    You’ll need to open it as an admin to modify repositories.

  2. Add the PSGallery repository

    Run this in PowerShell:

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

    For running Register-PSRepository with “-Default” switch, you won’t need any source locations.

  3. (OPTIONAL): Avoid naming conflicts!

    I mean, it’s not technically optional… But you only need to do this, if you get an error about “PSGallery” already existing.

    So, in case 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!

Notes

Even though you originally got an error about a missing repository, you might still be blocked from adding the repository with a proper URL because one with an almost matching URL exists – in that case, you need to run this:

Unregister-PSRepository -Name [offending repository name]
mm
5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments