0

I'm new to PowerShell, and I'm tired of changing proxy settings and proxy authentication every time I go to the university, is there a way to do that using PowerShell Commands , like using PowerShell script to enter the proxy address and the port and the authentication, instead of using the graphical windows InternetOptions>Connections>LanSettings (I assume that exists), if yes how can I do it?

I've searched the web and StackOverflow but I had different answers that didn't target what I want.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

A gotcha: Proxy settings are initialized when wininet layer is loaded, so these won't take effect immidiently. You can create an iexplore com object and destroy it to force the wininet layer reload.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.