9

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 ?

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

4 Answers 4

11

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 !

1
  • 1
    should there be a check for "ProxyEnable"?
    – vxsx
    Mar 14, 2016 at 12:48
10

[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.

4
  • 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, 2017 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, 2017 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, 2017 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, 2017 at 8:55
2

Same host:

Get-ItemProperty -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

source: https://www.mowasay.com/2016/08/windows-check-proxy-settings-from-powershell/

Remote host(s):

Invoke-Command -ComputerName Member01, Server01 {Get-ItemProperty -Path 'HKLM:\SOFTWARE\VMware, Inc.\VMware Tools\' -Name InstallPath | select InstallPath}

source: http://vcloud-lab.com/entries/powershell/powershell-get-registry-value-data

1

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
3
  • 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, 2015 at 6:39
  • 1
    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. Jun 3, 2015 at 7:35
  • This is definitely a better answer for simple use cases. Thanks :)
    – Pytry
    Aug 16, 2019 at 21:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .