Recovery of the PowerShell PSGallery behind a proxy

Many applications assume that you have a clear, direct route to the internet, however in a Corporate environment the chances are you have a proxy you have to traverse.

Unfortunately, PowerShell PsGallery is no exception.

If you run Get-PSRepository on a clean installation of Windows 10 whilst behind a proxy, you will get the warning message:

WARNING: Unable to find module repositories.

Normally, you will need to run the following command to recover the missing PsRepository:

Register-PSRepository -Default -Verbose

The command works without complaining, with just a warning suggesting that something might have gone wrong:

VERBOSE: Performing the operation "Register Module Repository." on target "Module Repository 'PSGallery' () in provider 'PowerShellGet'.".

Again, running Get-PSRepository returns an empty result set and the usual warning:

WARNING: Unable to find module repositories.

The issue here is that the cmdlet Register-PsRepository assumes that you can connect directly to the internet without using a proxy, tries to do so, fails to connect and then throws up a meaningless error message – not great for troubleshooting!

To fix this, you need to configure your default proxy settings in your powershell profile. Start powershell and run the following:

notepad $PROFILE

This will start notepad and open your powershell profile. If the file doesn’t exist, Notepad will prompt you to create it.

Add these lines to the profile script:

[system.net.webrequest]::defaultwebproxy = new-object system.net.webproxy('http://proxyaddress:portnumber')
[system.net.webrequest]::defaultwebproxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true

Save, close and restart powershell.

Now, you can register the default PsRepository with this command:

Register-PSRepository -Default

If you query the registered repositories, you will now see the default PsRepository:

Get-PSRepository
 
Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Untrusted            https://www.powershellgallery.com/api/v2/

 

No comments yet.

Leave a Reply