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\126.96.36.199\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
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? :)
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.
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!