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 ?

Ob1lan
  • 190
  • 2
  • 5
  • 15

4 Answers4

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 !

Ob1lan
  • 190
  • 2
  • 5
  • 15
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.

peterh
  • 4,953
  • 13
  • 30
  • 44
aDoN
  • 201
  • 2
  • 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 '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 08: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 08: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 08: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

Yumi Koizumi
  • 121
  • 1
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
Kirill Pashkov
  • 163
  • 2
  • 9
  • 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 03 '15 at 06: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. – Kirill Pashkov Jun 03 '15 at 07:35
  • This is definitely a better answer for simple use cases. Thanks :) – Pytry Aug 16 '19 at 21:54