I was interested, so I wrote a little POSH CmdLet that should help. There were plenty of references via a search on Google for how to do this so the information was there. Most of the solutions I found weren't really in the standard Powershell coding convention so I couldn't help myself. Try this out. This does nothing for the "Automatically Detect Settings". You are on your own for that one. This does however Enable/Disable proxy settings:
function Modify-ProxySettings() {
[CmdLetBinding(SupportsShouldProcess=$True)]
Param (
[Parameter(Mandatory=$False)][String]$Proxy,
[Parameter(Mandatory=$False)][String]$Port,
[ValidateSet("Disable","Enable")]
[Parameter(Mandatory=$True)][String]$Action
)
Begin {
$RegKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
if ($Action.Equals("Enable") -and ([String]::IsNullOrEmpty($Proxy) -and [String]::IsNullOrEmpty($Port))) {
throw New-Object System.Exception "Proxy and Port must be defined when enabling"
}
}
Process {
if ($Action.Equals("Enable")) {
Set-ItemProperty -Path $RegKey -Name ProxyEnable -Value 1
Write-Verbose -Message "Set: $RegKey\ProxyEnable to Enabled(1)"
Set-ItemProperty -Path $RegKey -Name ProxyServer -Value "$Proxy`:$Port"
Write-Verbose -Message "Set: $RegKey\ProxyServer to $Proxy`:$Port"
Write-Host "Proxy Enabled with $Proxy`:$Port"
} elseif ($Action.Equals("Disable")) {
Set-ItemProperty -Path $RegKey -Name ProxyEnable -Value 0
Write-Verbose -Message "Set: $RegKey\ProxyEnable to Disabled(0)"
Set-ItemProperty -Path $RegKey -Name ProxyServer -Value ""
Write-Verbose -Message "Proxy server and port removed"
Write-Host "Proxy Disabled"
}
}
}
Usage:
Modify-ProxySettings -Action Disable #Disables
Modify-ProxySettings -Action Enable -Proxy someproxy.com -Port 1337 #Enables
Some validation catches:
- Action is mandatory and only takes "Disable" or "Enable". Use tab completion for simplicity
- Proxy and Port are required if "Enable" is chosen