6 minute read

The following powershell script toggles the Internet Explorer proxy status between the following values:

  • Automatically detect settings
  • Explicit Proxy Server

remember that after executing the script, Internet Explorer must be restarted.

this script is a widely based on the work of the following guys (thank you!):

I tested this script with Internet Explorer 11 on Windows 8.1, but I think that is shoud work with previous versions too…

<p><font face="Courier New"># This function toggle between the following Internet Explorer Settings     <br /> # Explicit Proxy Disabled and automatically detect proxy set to ON      <br /> # Explicit proxy Enabled and automatically detect proxy set to OFF</font></p> <font face="Courier New"></font>  <p><font face="Courier New">#provide your proxy here     <br /></font><font face="Courier New">$proxyServerToDefine = <strong><font style="background-color: rgb(255, 255, 0);">&quot;99.99.99.99:80&quot;</font></strong></font></p> <font face="Courier New"></font>  <p><font face="Courier New">function Set-AutomaticallyDetectProxySettings ($enable)     <br /> {      <br />&#160;&#160;&#160; # Read connection settings from Internet Explorer.      <br />&#160;&#160;&#160; $regKeyPath = &quot;HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections\&quot;      <br />&#160;&#160;&#160; $conSet = $(Get-ItemProperty $regKeyPath).DefaultConnectionSettings      <br />&#160; <br />&#160;&#160;&#160; # Index into DefaultConnectionSettings where the relevant flag resides.      <br />&#160;&#160;&#160; $flagIndex = 8      <br />&#160; <br />&#160;&#160;&#160; # Bit inside the relevant flag which indicates whether or not to enable automatically detect proxy settings.      <br />&#160;&#160;&#160; $autoProxyFlag = 8      <br />&#160; <br />&#160;&#160;&#160; if ($enable)      <br />&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if ($($conSet[$flagIndex] -band $autoProxyFlag) -eq $autoProxyFlag)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; else      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Write-Host &quot;Enabling 'Automatically detect proxy settings'.&quot;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $conSet[$flagIndex] = $conSet[$flagIndex] -bor $autoProxyFlag      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $conSet[4]++      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Set-ItemProperty -Path $regKeyPath -Name DefaultConnectionSettings -Value $conSet      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160; }      <br />&#160;&#160;&#160; else      <br />&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if ($($conSet[$flagIndex] -band $autoProxyFlag) -eq $autoProxyFlag)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; # 'Automatically detect proxy settings' was enabled, adding one disables it.      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Write-Host &quot;Disabling 'Automatically detect proxy settings'.&quot;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $mask = -bnot $autoProxyFlag      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $conSet[$flagIndex] = $conSet[$flagIndex] -band $mask      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $conSet[4]++      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Set-ItemProperty -Path $regKeyPath -Name DefaultConnectionSettings -Value $conSet      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160; }</font></p> <font face="Courier New"></font>  <p><font face="Courier New">&#160;&#160;&#160;&#160; $conSet = $(Get-ItemProperty $regKeyPath).DefaultConnectionSettings     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if ($($conSet[$flagIndex] -band $autoProxyFlag) -ne $autoProxyFlag)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Write-Host &quot;'Automatically detect proxy settings' is disabled.&quot;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; else      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Write-Host &quot;'Automatically detect proxy settings' is enabled.&quot;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br /> }</font></p> <font face="Courier New"></font>  <p><font face="Courier New">$regKey=&quot;HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings&quot;     <br /> $proxyServer = &quot;&quot;      <br /> Write-Host &quot;Retrieve the proxy server ...&quot;      <br /> $proxyServer = Get-ItemProperty -path $regKey ProxyServer -ErrorAction SilentlyContinue      <br /> Write-Host $proxyServer      <br />if([string]::IsNullOrEmpty($proxyServer))      <br /> {      <br />&#160;&#160;&#160; Write-Host &quot;Proxy is actually disabled&quot;      <br />&#160;&#160;&#160; Set-AutomaticallyDetectProxySettings ($false)      <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; Set-ItemProperty -path $regKey ProxyEnable -value 1      <br />&#160;&#160;&#160; Set-ItemProperty -path $regKey ProxyServer -value $proxyServerToDefine      <br />&#160;&#160;&#160; Write-Host &quot;Proxy is now enabled&quot;      <br /> }      <br /> else      <br /> {      <br />&#160;&#160;&#160; Write-Host &quot;Proxy is actually enabled&quot;      <br />&#160;&#160;&#160; Set-AutomaticallyDetectProxySettings ($true)</font></p> <font face="Courier New"></font>  <p><font face="Courier New">&#160;&#160;&#160; Set-ItemProperty -path $regKey ProxyEnable -value 0     <br />&#160;&#160;&#160; Remove-ItemProperty -path $regKey -name ProxyServer      <br />&#160;&#160;&#160; Write-Host &quot;Proxy is now disabled&quot;      <br /> }</font></p>