Powershell doesn’t seem to respect the default proxy settings carried through from the OS/Browser (possibly a bug like this one or this one, if you feel like reading). If you to fetch content from a website and your corporate network has a proxy in place, you’ll get an error like this one:
PS > Invoke-WebRequest "http://www.contoso.com"
Invoke-WebRequest:
Proxy Web Gateway - Notification
Request Blocked
(Insert scary message about trying to circumvent your corporate proxy here and you'll get in super-big trouble if this is malicious. For real, don't do it!) If you're not trying to break the rules, please contact your helpdesk for assistance.
URL: break_line("http://www.contoso.com/");
You can resolve this by specifying the proxy settings as parameters on your request:
PS > Invoke-WebRequest "http://www.contoso.com" -Proxy "http://proxy.yourcorp.com:8080" -ProxyUseDefaultCredentials
StatusCode : 200
StatusDescription : OK
Content : <html><head><title>Microsoft Corporation</title><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"></meta <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta><meta name="...(and so on)...
However, you don’t have to specify that proxy settings on every single request – you can just set the default proxy settings in your Powershell session. The first line below sets the default proxy, and the second line tells it use the currently logged-in user to access the proxy (this isn’t necessary if your proxy doesn’t require login, but most do):
PS > [System.Net.Http.HttpClient]::DefaultProxy = New-Object System.Net.WebProxy('http://proxy.yourcorp.com:8080', $true)
PS > [System.Net.Http.HttpClient]::DefaultProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
Once you’ve set those proxy settings for the default client, you can make a regular old web call without specifying the proxy details:
PS > Invoke-WebRequest "http://www.contoso.com"
StatusCode : 200
StatusDescription : OK
Content : <html><head><title>Microsoft Corporation</title><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"></meta <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta><meta name="...(and so on)...
But if you’re the kind of person who doesn’t like setting your proxy settings every single time you launch Powershell, you’re in luck! Powershell, like other shells, has a default profile script that it runs when you log in and you can add the lines above to that script and they’ll execute by default every time you launch Powershell. When you try to open it, it will even create a blank script for you if one doesn’t already exist:
PS > notepad $PROFILE
Then just dump in the contents of the commands above, plus a little note to remind you that you set this up so if you have problems later, you’ll know where to check 🙂
# Set proxy settings manually since it doesn't pick it up from IE
[System.Net.Http.HttpClient]::DefaultProxy = New-Object System.Net.WebProxy('http://proxy.yourcorp.com:8080', $true)
[System.Net.Http.HttpClient]::DefaultProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
Write-Output "Proxy configuration set via profile. To update, run 'notepad `$PROFILE' at the PS prompt."