9

I need to enable/disable IE proxy settings while IE is running. I have a PowerShell script line to enable the proxy:

Set-ItemProperty -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ProxyEnable -value 1


or this to disable:

Set-ItemProperty -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ProxyEnable -value 0


Above scripts work, registry key gets updated. However, IE doesn't pick up the value until I close all the open IE windows and open a new one. I need already opened/running IE windows to pick up the new setting.

Would there be any way to achieve what I want?

vonPryz
  • 22,996
  • 7
  • 54
  • 65
Oscar
  • 233
  • 2
  • 6
  • 15

3 Answers3

9

The problem is that IE won't reset the proxy settings until it either

  1. closes, or
  2. has its configuration refreshed.

Below is the code that I've used to get this working:

function Refresh-System
{
  $signature = @'
[DllImport("wininet.dll", SetLastError = true, CharSet=CharSet.Auto)]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
'@

$INTERNET_OPTION_SETTINGS_CHANGED   = 39
$INTERNET_OPTION_REFRESH            = 37
$type = Add-Type -MemberDefinition $signature -Name wininet -Namespace pinvoke -PassThru
$a = $type::InternetSetOption(0, $INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
$b = $type::InternetSetOption(0, $INTERNET_OPTION_REFRESH, 0, 0)
return $a -and $b
}
  • 1
    I've been looking all over for how to update the system settings in the browser after setting the registry. The two calls to InternetSetOptions did the trick - thanks man! – stevejoy32 Aug 03 '17 at 16:35
  • 1
    This is the only method I have found that allows me apply proxy changes when automating builds with tools like WinRM and Packer with cloud providers like AWS. In these situations you often don't have access to the UI and IE has never been started so the proxy settings are not be applied. You have my upvote, sir. ty – Andrew Hart Mar 22 '18 at 17:28
4

modifying the proxy value under

[HKEY_USERS\<your SID>\Software\Microsoft\Windows\CurrentVersion\Internet Settings]

doesnt need to restart ie

Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
0

I know this is an old question, however here is a simple one-liner to switch it on or off depending on its current state:

set-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'  -name ProxyEnable -value (-not ([bool](get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'  -name ProxyEnable).proxyenable))
GMasucci
  • 2,834
  • 22
  • 42