5

I have to retrieve the proxy server address and port via PowerShell, so I can use the Invoke-Webrequest -uri http://some-site.com -Proxy command. The expected output should looks like http://proxy-server.com:port.

I there a PowerShell function to retrieve the proxy server address and port, so we can use it in a script ?

  • Bear in mind that sometimes the definition of a proxy can be in a wpad.dat file. – user493650 Oct 26 '18 at 6:58
9

Here is the PowerShell function to achieve my goal :

function Get-InternetProxy
 { 
    <# 
            .SYNOPSIS 
                Determine the internet proxy address
            .DESCRIPTION
                This function allows you to determine the the internet proxy address used by your computer
            .EXAMPLE 
                Get-InternetProxy
            .Notes 
                Author : Antoine DELRUE 
                WebSite: http://obilan.be 
    #> 

    $proxies = (Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').proxyServer

    if ($proxies)
    {
        if ($proxies -ilike "*=*")
        {
            $proxies -replace "=","://" -split(';') | Select-Object -First 1
        }

        else
        {
            "http://" + $proxies
        }
    }    
}

enter image description here

Hope this helps !

  • should there be a check for "ProxyEnable"? – vxsx Mar 14 '16 at 12:48
7

[System.Net.WebProxy]::GetDefaultProxy() | select address

[System.Net.WebProxy] is an object and one of its static methods is GetDefaultProxy(). The select shows for us from all the columns, what interests us, and it is the address.

  • 1
    Thanks. But it looks a little bit cryptic, maybe a little bit of explanation could make it more useful for the readers. – peterh Feb 13 '17 at 17:26
  • [System.Net.WebProxy] it's an object and one of its static methods is GetDefaultProxy(), then from all the columns, what interest us is the address. So that line just solves the problem. Hope it's crystal clear now ;) – aDoN Feb 14 '17 at 8:16
  • To me it was clear even without your comment. To the readers of the future, not surely. The site has a complex review process, and such single-line snippets mostly don't pass it. My advice tried to save your post (note, I've found it by reviewing the "low quality posts" queue). – peterh Feb 14 '17 at 8:52
  • Furthermore, such comments which are essentially extensions of an answer, should be edited into the answer and not into a comment below. I inserted your comment into your answer in the here required style, and voted it up because so it already deserves it. :-) – peterh Feb 14 '17 at 8:55
0

You first get the IE settings. Then make use of proxy settings if they are active.

if ($IESettings.ProxyEnable -eq 1)

$IESettings = Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'
$Proxy = "http://$(($IESettings.ProxyServer.Split(';') | ? {$_ -match 'ttp='}) -replace '.*=')"
Invoke-WebRequest 'http://some-site.com' -ProxyUseDefaultCredentials -Proxy $Proxy
  • Thanks for your participation, but this does not work if you configured different proxies addresses (HTTP, HTTPS, FTP). The function I posted as answer will work for both scenario : one proxy for all protocols, or different proxies. – Ob1lan Jun 3 '15 at 6:39
  • Actually, your script returns only HTTP proxy address in any case, even if you are trying to reach HTTPS page. So it does about the same as my solution. Also, your function can be missleading in case where differet proxy servers used for different types and there is no proxy for HTTP at all, since your function returns '-First 1'. I slighty changed my previous post to cover the case where there is several servers for different types. – Kirill Pashkov Jun 3 '15 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.