Recovering the PsGallery Repository behind a Corporate Proxy
While getting a new workstation is usually nice, reinstalling all your softwares and settings is definitely not the most pleasant thing that comes to my mind. One of the factors that can contribute the most to making the process even less pleasant is working around the corporate proxy.
Many applications live on the assumption that nobody uses proxy servers, thus making online repositories inaccessible for new installations and for automatic updates.
Unfortunately, PsGallery is no exception.
If you run Get-PSRepository
on a vanilla installation of Windows 10 behind a corporate proxy, you will get a warning message:
WARNING: Unable to find module repositories.
After unleashing my Google-Fu, I learned that I had 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 problem here is that the cmdlet Register-PsRepository
assumes that you can connect directly to the internet, without using a proxy, so it tries to do so, fails to connect and does not throw a meaningful error messsage. Thank you, Register-PsRepository
, much appreciated!
In order to fix it, 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://YourProxyHostNameGoesHere:ProxyPortGoesHere') [system.net.webrequest]::defaultwebproxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials [system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true
Save, close and restart powershell (or execute the profile script with iex $PROFILE
).
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/
Horray!
Posted on December 19, 2017, in PowerShell and tagged Powershell PsGallery Proxy. Bookmark the permalink. 34 Comments.
Great article, I just spent all morning trying to install the AzureAD module on a Windows 2016 server. This has resolved it as I am going through a proxy server.
Thank you so much. I have been chasing my tail until I found your article. It worked like a charm.
thanks!
Awesome! Many thanks!
Many thanks!
Awesome guide. It worked to me.
You are amazing! Worked perfectly.
Thank you.
I believe this was the final solution to my 12 step problem to get NUGET working behind a firewall 😉 Thanks!
back of the net!!
Thanks for the article. It saved my day.
How do i know what my proxy settings are? ON local systems i don’t have any enabled, but i am on a corporate AD system which could be using one.. is there any way to tell what it is?
This line of powershell tells you what the proxy settings are on the current machine:
(Get-ItemProperty -Path “Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings”).ProxyServer
You could run it on the target machine to see what it gives.
Hope this helps
thank you so so much, I wasted half a morning on this
OMG! I was looking soooooooo long for this solution, finally I found this site! 😀
Thanks
Thanks, Was banging my head on this one for a while. I knew it was the web proxy!
Thanks buddy, this helped a lot. it was a headache for a while.
Thanks mate for sharing this, now raises the questions for the users who are restricted behind the Internet terminal server and not the proxy server, how are they able to get their powershell configured for getting around the terminal server ?
I’m sorry,I don’t even know what internet terminal server is…
Well, in some IT organizations to consolidate the internal network security (Usaully applied for IT Admins) another layer, shields and prevents the internal clients from directly accessing the proxy server. Instead these clients establish a connection to the Terminal Server (The server is used for application or Internet distribution among internal clients) through the RDP and this Terminal Server is in its turn placed behind the proxy server. Here is the exact scenarion which i am dealing with 😉 . Now i am at the point of finding a way to fool the Terminal server in the first place then take care of the proxy server.. Anyway thanks for the response my friend
Thanks worked like a charm!
At last, an article I can understand! Thanks for explaining it clearly and concisely.
No thanks to Microsoft for making it so hard (and difficult to troubleshoot).
Yes, I LIVE BEHIND A PROXY!!!!
You need to note:
After “notepad $PROFILE” you have to close all Powershell Editors or windows and start as administrator the powershell editor again!
…don’t forget save your ps – scripts before!
Actually it’s enough to re run the profile script, as noted in the article
This Article should be the first result on Google search
Thank you!!
Thank you so much guy for sharing this !!! Sharing is caring 🙂
If the above doesn’t work – try running this. MS Disabled TLS1.0 & 1.1 support to their gallery earlier in the year
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
May also need to to turn off FIPS Cryptography using the Local Policies
Can reenable after registering module
Thanks so much for sharing this! I’m new to PS and was tearing my hair out trying to figure out why I was getting nothing but angry red errors trying to to do anything regarding repositories from a server sitting behind a proxy.
Awesome stuff, I have been working on this for a while and this solved my issue downloading Powershell modules within our corporate environment.
it worked super 🙂
Thank you, since I learned something new, thought I’d share.
You do not need to restart powershell, instead call . $profile # to reload the profile script
Pingback: Offline WIM Servicing – WIP – ConfigMgrUK
Pingback: SCOMHelper PowerShell Module – A SCOM Admin’s Best Friend - The Monitoring Guys