PowerShell behind a proxy server
Recently I’ve been trying to get some PowerShell deployment scripts working from behind a proxy server. There’s a lot of information all over the internet about this, but most of it does not apply if you don’t directly control the commands you want to execute, for example if your connections happen within a third party DLL. Normally for C# code you would solve this using an App.config or a Web.config, but obviously PowerShell doesn’t have this.
Anyway, after a lot of dredging around, I finally found the solution. Simply execute the following two commands, and all further connections in the session will use the proxy server transparently:
netsh winhttp import proxy source=ie (New-Object System.Net.WebClient).Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
Taking this one step further, if you want to automatically run these commands every time you open the PowerShell command line, add them to the file returned by the $PROFILE variable:
PS> $PROFILE C:\Users\jgrant\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Just open or create this file in Notepad or your favourite text editor and paste in those two lines above.
One Reply to “PowerShell behind a proxy server”