NicolTIP#031- Toggle Internet Explorer proxy via powershell
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);">"99.99.99.99:80"</font></strong></font></p> <font face="Courier New"></font> <p><font face="Courier New">function Set-AutomaticallyDetectProxySettings ($enable) <br /> { <br />    # Read connection settings from Internet Explorer. <br />    $regKeyPath = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections\" <br />    $conSet = $(Get-ItemProperty $regKeyPath).DefaultConnectionSettings <br />  <br />    # Index into DefaultConnectionSettings where the relevant flag resides. <br />    $flagIndex = 8 <br />  <br />    # Bit inside the relevant flag which indicates whether or not to enable automatically detect proxy settings. <br />    $autoProxyFlag = 8 <br />  <br />    if ($enable) <br />    { <br />         if ($($conSet[$flagIndex] -band $autoProxyFlag) -eq $autoProxyFlag) <br />        { <br />        } <br />        else <br />        { <br />            Write-Host "Enabling 'Automatically detect proxy settings'." <br />             $conSet[$flagIndex] = $conSet[$flagIndex] -bor $autoProxyFlag <br />            $conSet[4]++ <br />            Set-ItemProperty -Path $regKeyPath -Name DefaultConnectionSettings -Value $conSet <br />         } <br />    } <br />    else <br />    { <br />        if ($($conSet[$flagIndex] -band $autoProxyFlag) -eq $autoProxyFlag) <br />        { <br />            # 'Automatically detect proxy settings' was enabled, adding one disables it. <br />            Write-Host "Disabling 'Automatically detect proxy settings'." <br />            $mask = -bnot $autoProxyFlag <br />             $conSet[$flagIndex] = $conSet[$flagIndex] -band $mask <br />            $conSet[4]++ <br />            Set-ItemProperty -Path $regKeyPath -Name DefaultConnectionSettings -Value $conSet <br />        } <br />    }</font></p> <font face="Courier New"></font> <p><font face="Courier New">     $conSet = $(Get-ItemProperty $regKeyPath).DefaultConnectionSettings <br />        if ($($conSet[$flagIndex] -band $autoProxyFlag) -ne $autoProxyFlag) <br />        { <br />            Write-Host "'Automatically detect proxy settings' is disabled." <br />        } <br />         else <br />        { <br />            Write-Host "'Automatically detect proxy settings' is enabled." <br />        } <br /> }</font></p> <font face="Courier New"></font> <p><font face="Courier New">$regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" <br /> $proxyServer = "" <br /> Write-Host "Retrieve the proxy server ..." <br /> $proxyServer = Get-ItemProperty -path $regKey ProxyServer -ErrorAction SilentlyContinue <br /> Write-Host $proxyServer <br />if([string]::IsNullOrEmpty($proxyServer)) <br /> { <br />    Write-Host "Proxy is actually disabled" <br />    Set-AutomaticallyDetectProxySettings ($false) <br />    <br />    Set-ItemProperty -path $regKey ProxyEnable -value 1 <br />    Set-ItemProperty -path $regKey ProxyServer -value $proxyServerToDefine <br />    Write-Host "Proxy is now enabled" <br /> } <br /> else <br /> { <br />    Write-Host "Proxy is actually enabled" <br />    Set-AutomaticallyDetectProxySettings ($true)</font></p> <font face="Courier New"></font> <p><font face="Courier New">    Set-ItemProperty -path $regKey ProxyEnable -value 0 <br />    Remove-ItemProperty -path $regKey -name ProxyServer <br />    Write-Host "Proxy is now disabled" <br /> }</font></p>