Using PowerShell to programmatically configure Internet Explorer proxy settings to work before it has been opened
I came to the same result as Richard's (slightly shorter) whilst setting proxy for Server Core 2016. Horrific that this is necessary!
function Set-Proxy($proxy, $bypassUrls){ $proxyBytes = [system.Text.Encoding]::ASCII.GetBytes($proxy) $bypassBytes = [system.Text.Encoding]::ASCII.GetBytes($bypassUrls) $defaultConnectionSettings = [byte[]]@(@(70,0,0,0,0,0,0,0,11,0,0,0,$proxyBytes.Length,0,0,0)+$proxyBytes+@($bypassBytes.Length,0,0,0)+$bypassBytes+ @(1..36 | % {0})) $registryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" Set-ItemProperty -Path $registryPath -Name ProxyServer -Value $proxy Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value 1 Set-ItemProperty -Path "$registryPath\Connections" -Name DefaultConnectionSettings -Value $defaultConnectionSettings netsh winhttp set proxy $proxy bypass-list=$bypassUrls}Set-Proxy "someproxy:1234" "*.example.com;<local>"
I figured it out.
The key part missing was a hex byte to indicate the length of the proxy address.
So I changed the line that concatinates the hex string to this:
$regString="46,00,00,00,00,00,00,00,0b,00,00,00,"+(%{[System.Convert]::ToString($proxy.length,16)})+",00,00,00," + $proxystring + (%{[System.Convert]::ToString($bypassList.length,16)}) + ",00,00,00," + $bypassString + "00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00"
And that works successfully.
So for a belt and braces approach:netsh
netsh winhttp set proxy $proxy bypass-list=$bypassList
Internet explorer for system accounts:
foreach ($account in "LOCALSYSTEM","NETWORKSERVICE","LOCALSERVICE") { & C:\windows\System32\bitsadmin.exe /Util /SetIEProxy $account Manual_proxy $proxy $bypassList}
Internet explorer for current accounts:
$proxyString = ""for ($i = 0;$i -lt (([System.Text.Encoding]::Unicode.GetBytes($proxy)).length); $i++) { if ($i % 2 -eq 0) { $byte = (([System.Text.Encoding]::Unicode.GetBytes($proxy))[$i]) $convertedByte=%{[System.Convert]::ToString($byte,16)} $proxyString = $proxystring + $convertedByte + "," }}$bypassString = ""for ($i = 0;$i -lt (([System.Text.Encoding]::Unicode.GetBytes($bypassList)).length); $i++) { if ($i % 2 -eq 0) { $byte = (([System.Text.Encoding]::Unicode.GetBytes($bypassList))[$i]) $convertedByte=%{[System.Convert]::ToString($byte,16)} $bypassString = $bypassString + $convertedByte + "," }}$regString="46,00,00,00,00,00,00,00,0b,00,00,00,"+(%{[System.Convert]::ToString($proxy.length,16)})+",00,00,00," + $proxystring + (%{[System.Convert]::ToString($bypassList.length,16)}) + ",00,00,00," + $bypassString + "00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00"$regstringAsArray = ("0x"+$regString.replace(",",",0x")).Split(",")$reg = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"Set-ItemProperty -Path $reg -Name ProxyServer -Value $proxySet-ItemProperty -Path $reg -Name ProxyEnable -Value 1Set-ItemProperty -Path $reg -Name ProxyOverride -Value $bypassList$reg = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections"Set-ItemProperty -Path $reg -Name DefaultConnectionSettings -Type Binary -Value $regstringAsArraySet-ItemProperty -Path $reg -Name SavedLegacySettings -Type Binary -Value $regstringAsArray