Scripting : Toggle proxy server in IE settings with PowerShell


My current Customer use a proxy server for Internet and I need to change this settings each morning (when I start to work for him) and each evening (when I return at home). To save precious time, I wrote a little script that toggle this setting at each script execution :

$regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
$proxyServer = ""
$proxyServerToDefine = "{Proxy}:{Port}"


Write-Host "Retrieve the proxy server ..."
$proxyServer = Get-ItemProperty -path $regKey ProxyServer -ErrorAction SilentlyContinue

Write-Host $proxyServer

if([string]::IsNullOrEmpty($proxyServer))
{
    Write-Host "Proxy is actually disabled"
    Set-ItemProperty -path $regKey ProxyEnable -value 1
    Set-ItemProperty -path $regKey ProxyServer -value $proxyServerToDefine
    Write-Host "Proxy is now enabled"
}
else
{
    Write-Host "Proxy is actually enabled"
    Set-ItemProperty -path $regKey ProxyEnable -value 0
    Remove-ItemProperty -path $regKey -name ProxyServer
    Write-Host "Proxy is now disabled"
}

Hope this helps!

Comments (18)

  1. Anonymous says:

    i want to know how can i use the code?

  2. hi,

    If you have PowerShell installed on your computer, just copy and paste this code in a file and save it as .ps1.

    Then you just have to right click and select execute on this file.

    You will find more information here : technet.microsoft.com/…/ee176949.aspx

  3. Anonymous says:

    Hi,

    I noticed that in order to make IE get the new registry settings, I have to restart IE. That is most annoying when I have many web page open and I simply connect to a new network that requires proxy. Did you solve this problem as well?

  4. Hi Paolo,

    Good point!

    Proxy settings are initialized when wininet layer is loaded and if you change the proxy it will be not applied for current session, so you need to restart IE by closing all your IE windows.

    So I don't think we can bypass this behavior.

    You will find some technical explanations here : msdn.microsoft.com/…/aa384075(v=vs.85).aspx

    And here :

    blogs.msdn.com/…/understanding-connection-limits-and-new-proxy-connection-limits-in-wininet-and-internet-explorer.aspx

    If you have a solution, don't hesitate to give it to me ! 🙂

  5. Anonymous says:

    OK. I'll dig to find one.

    Meanwhile, there is also the need to include Enabling / Disabling of proxy in Skype. This is a flag to set in a file C:Users<your_alias>Applcation DataSkypeshared.xml (I hate those applications not using the registry; it reminds me about the nightmare of win.ini and system.ini)

    <HttpsProxy>

           <Addr>xx.xx.xx.xx:yyyy</Addr>

           <Enable>1</Enable>

           <Pwd>RRtYRXVyhNzg=</Pwd>

           <User>domainuser</User>

    </HttpsProxy>

    Any suggestion on how to write in that xml file from PowerShell script?

  6. Hi Paolo,

    You can edit XML file with PowerShell.

    You will find some resources / threads on this topic :

    You can also make 2 config file for skype and change their name with a simple DOS command in order to apply or not proxy settings for Skype.

  7. Anonymous says:

    You can get IE to update the proxy configuration settings without having to reopen the IE sessions.  I have it working with this setup:

    $ProxyServer = "server.domain.com"

    $ProxyPort     = "1234"

    $Path   = "HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet Settings"

    $Proxy = $ProxyServer + ":" + $ProxyPort

    Enable an explicit proxy

    Set-ItemProperty -Path $path -Name ProxyEnable -Value 1          #Enable the explicit proxy

    Set-ItemProperty -Path $path -Name ProxyServer -Value $Proxy #Configure the server settings

    Disable an explicit proxy

    Set-ItemProperty -Path $path -Name ProxyEnable -Value 0 #Disable the explicit proxy

  8. Anonymous says:

    If you don't mind cheating a bit you can create a ComObject for Internet Explorer and then end it. This will also update the proxy config. I added this code after setting the reg value and it works like a charm.

    $ie = new-object -ComObject internetexplorer.application
    
    $ie.Quit()
    
  9. Anonymous says:

    Without powershell for more basic users  – Double click the bat file to toggle.

    @echo off

    IF "%1" == "" goto toggle

    IF "%1" == "1" goto turnon

    goto turnoff

    :toggle

    REG QUERY "HKCUSoftwareMicrosoftWindowsCurrentVersionInternet Settings" /v "ProxyEnable" | Find "0x0"

    IF %ERRORLEVEL% == 1 goto turnoff

    If %ERRORLEVEL% == 0 goto turnon

    goto end

    :turnon

    REG ADD "HKCUSoftwareMicrosoftWindowsCurrentVersionInternet Settings" /v ProxyEnable /t REG_DWORD /f /D 1

    echo "PROXY ON"

    goto end

    :turnoff

    REG ADD "HKCUSoftwareMicrosoftWindowsCurrentVersionInternet Settings" /v ProxyEnable /t REG_DWORD /f /D 0

    echo "PROXY OFF"

    goto end

    :end

    @exit

  10. Anonymous says:

    Wow, nice stuff..

    i modified it using ProxyScript

    new-itemproperty ….  -Name AutoConfigURL -Value "path of script

    and

    remove-itemproperty ….

  11. Anonymous says:

    You should try this tool. It is a really cool proxy switcher.

    gallery.technet.microsoft.com/Switch-IE-Proxy-b525ae0d

  12. Anonymous says:

    nice little script, thanks

  13. Anonymous says:

    @Ash : Excellent!!! Your script is working great. Exactly what I was looking for.

  14. Anonymous says:

    I'm not able to change the IE privacy settings using powershell. Please suggest.

  15. Anonymous says:

    Not so good.

    This wipes out all the items in the exceptions list!

  16. BM says:

    @BK Which one?

  17. jvdp81 says:

    If you don't want to have to restart IE you can add this to your Powershell script:

    $source=@"

    [DllImport("wininet.dll")]

    public static extern bool InternetSetOption(int hInternet, int dwOption, int lpBuffer, int dwBufferLength);  

    "@

    #Create type from source

    $wininet = Add-Type -memberDefinition $source -passthru -name InternetSettings

    #INTERNET_OPTION_PROXY_SETTINGS_CHANGED

    $wininet::InternetSetOption([IntPtr]::Zero, 95, [IntPtr]::Zero, 0)|out-null

    #INTERNET_OPTION_REFRESH

    $wininet::InternetSetOption([IntPtr]::Zero, 37, [IntPtr]::Zero, 0)|out-null

  18. Kadhri says:

    Wow….

    This is the script i am actually looking for

    i need something to do…I have 150 computer in my work place.We are using to two proxies

    here i want to query what proxies are using by users

    and also want to change the proxy address to a default one, also want to disable changing proxy address, is it possible

    PS: this is not domain network

Skip to main content