This post was most recently updated on November 16th, 2021.2 min read.
This article explains how to fix an issue when updating a module in PowerShell. The error is something along the lines of: “System.InvalidOperationException: PowerShell Gallery is currently unavailable. Please try again later.”
The whole error looks something like the one below, although you can probably get it for many different commandlets.
Failed updating module 'SharePointPnPPowerShellOnline' version 3.19.2003.0. Update the module and then try again. Error Could not execute Command. The error: System.InvalidOperationException: PowerShell Gallery is currently unavailable. Please try again later.
The main point is the latter part of the error: PowerShell Gallery is currently unavailable. This, quite simply, stops you from installing or updating any modules, as you can’t access the PowerShell gallery, that hosts the modules.
Luckily, like usual, there are a few ways to fix this!
Time needed: 10 minutes.
How to fix “System.InvalidOperationException: PowerShell Gallery is currently unavailable.” in 1 to 5 easy steps
- Verify whether you have a recent enough PowerShell version
Get-Host | Select-Object Version
If it’s 5.1 or newer, you’re probably good! If not, download latest version here: https://www.microsoft.com/en-us/download/details.aspx?id=54616
- Verify you have the right repository
Run this commandlet in your PowerShell:
Your output is somewhat like this:
Name InstallationPolicy SourceLocation
---- ------------------ --------------
PSGallery Untrusted https://www.powershellgallery.com/api/v2
There’s 3 things that COULD be wrong in this case. Depending on the exact error message you got, either:
1) The source needs to be trusted (see step 3)
2) The SourceLocation is not accessible (see step 4)
3) The SourceLocation URI needs to end with ‘/’ (see step 5)
- Set your PSGallery to be trusted
Run the following commandlet:
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
If you get an error like this:
Get-PSGalleryApiAvailability : PowerShell Gallery is currently unavailable. Please try again later.
Then you can’t access the URI either because it doesn’t have a trailing slash (see step 5) or because it’s not accessible (see step 4).
And if you get an error like:
Register-PSRepository : Use 'Register-PSRepository -Default' to register the PSGallery repository.
Then, well, run:
To see if it helps!
- Verify you have access to the URI
Open up your browser and navigate to this address:
If you get something like the screenshot below, you access this address, but your PowerShell can’t.
In this case your PowerShell is probably unable to access it due to a mismatch in TLS versions – try running this commandlet to enable TLS 1.2 and see if it helps:
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
- Fix the PSGallery’s URI
In case the PSGallery URI does not end with a trailing slash, run the following commands (this’ll set the new PSRepository to be Trusted as well):
Register-PSRepository -Name PSGallery -SourceLocation "https://www.powershellgallery.com/api/v2/" -InstallationPolicy Trusted
If you get an error after running this, you can also try naming your Repository something else.
And with that, you should be good!
- Azure Function build fails with “Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: Microsoft.AspNetCore.Mvc.Core” - December 1, 2021
- How to update from deprecated Microsoft.Azure.Services.AppAuthentication to new and shiny Azure.Security.KeyVaults.Secrets? - November 23, 2021
- Fixing Blazor WebAssembly .NET 6 MSAL authorization bug - November 16, 2021