共用方式為


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

  • Anonymous
    May 30, 2013
    i want to know how can i use the code?

  • Anonymous
    May 31, 2013
    The comment has been removed

  • Anonymous
    July 04, 2013
    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?

  • Anonymous
    July 06, 2013
    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).aspxAnd here :blogs.msdn.com/.../understanding-connection-limits-and-new-proxy-connection-limits-in-wininet-and-internet-explorer.aspxIf you have a solution, don't hesitate to give it to me ! :)

  • Anonymous
    July 08, 2013
    The comment has been removed

  • Anonymous
    August 26, 2013
    Hi Paolo,You can edit XML file with PowerShell.You will find some resources / threads on this topic :blogs.technet.com/.../the-scripting-wife-learns-to-use-powershell-to-work-with-xml.aspxsocial.technet.microsoft.com/.../how-to-update-an-xml-element-value-using-powershell social.technet.microsoft.com/.../edit-xml-with-powershell 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.

  • Anonymous
    September 10, 2013
    The comment has been removed

  • Anonymous
    October 08, 2013
    The comment has been removed

  • Anonymous
    October 14, 2013
    Without powershell for more basic users  - Double click the bat file to toggle.@echo offIF "%1" == "" goto toggleIF "%1" == "1" goto turnongoto turnoff:toggleREG QUERY "HKCUSoftwareMicrosoftWindowsCurrentVersionInternet Settings" /v "ProxyEnable" | Find "0x0"IF %ERRORLEVEL% == 1 goto turnoffIf %ERRORLEVEL% == 0 goto turnongoto end:turnonREG ADD "HKCUSoftwareMicrosoftWindowsCurrentVersionInternet Settings" /v ProxyEnable /t REG_DWORD /f /D 1echo "PROXY ON"goto end:turnoffREG ADD "HKCUSoftwareMicrosoftWindowsCurrentVersionInternet Settings" /v ProxyEnable /t REG_DWORD /f /D 0echo "PROXY OFF"goto end:end@exit

  • Anonymous
    November 05, 2013
    Wow, nice stuff..i modified it using ProxyScriptnew-itemproperty ....  -Name AutoConfigURL -Value "path of scriptandremove-itemproperty ....

  • Anonymous
    November 07, 2013
    You should try this tool. It is a really cool proxy switcher.gallery.technet.microsoft.com/Switch-IE-Proxy-b525ae0d

  • Anonymous
    March 21, 2014
    nice little script, thanks

  • Anonymous
    June 10, 2014
    @Ash : Excellent!!! Your script is working great. Exactly what I was looking for.

  • Anonymous
    December 10, 2014
    I'm not able to change the IE privacy settings using powershell. Please suggest.

  • Anonymous
    June 03, 2015
    Not so good. This wipes out all the items in the exceptions list!

  • Anonymous
    October 05, 2015
    @BK Which one?

  • Anonymous
    October 19, 2015
    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

    • Anonymous
      May 21, 2016
      @jvdp81: You are my hero - Thanks! :-)
    • Anonymous
      November 21, 2016
      Wow! This works. Thanks. Now every time I run the command, IE automatically refreshes the proxy settings and I don't need to restart IE.
  • Anonymous
    November 01, 2015
    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

  • Anonymous
    May 04, 2016
    This is really useful

  • Anonymous
    March 15, 2017
    Thank you. Just saved me many mouse clicks a day :).

  • Anonymous
    July 19, 2017
    I just really wanted to say thank you. Helped me out a lot.