Set proxy in IE using Powershell

<#
.Synopsis
   Sets, enables or disables proxy server in Internet Explorer
.DESCRIPTION
   Sets or disables proxy server in Internet Explorer. Useful if one connects with his/her notebook to different networks.

   Leverages these registry settings:
   HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable (DWORD value 1 or 0)
   HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer (REG_SZ)
   HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyOverride (REG_SZ)

.EXAMPLE
   Set-IEProxy

   Enables proxy in IE and sets it to default values which are vproxy:3128, bypass proxy for "*.ds.mfcr.cz;172.27.*;172.16.65.126;<local>"
.EXAMPLE
   Set-IEProxy -Enable

   Enables proxy in IE. Whatever values are at the time in the registry are used.
.EXAMPLE
  Set-IEProxy -Disable

  Disables proxy in IE
.EXAMPLE
   Set-IEProxy -ProxyAddress myproxy -ProxyPort 8080 -BypassProxy "<localhost>"
#>
function Set-IEProxy
{
    [CmdletBinding(DefaultParameterSetName='Parameter Set 1', 
                  SupportsShouldProcess=$true)]

    
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$false,
                   ParameterSetName='Parameter Set 1',
                   ValueFromPipelineByPropertyName=$false,
                   ValueFromPipeline=$false,
                   Position=0)]
        [string]$ProxyAddress="vproxy",

        # Param2 help description
        [Parameter(Mandatory=$false,
                   ParameterSetName='Parameter Set 1',
                   ValueFromPipelineByPropertyName=$false,
                   ValueFromPipeline=$false,
                   Position=1)]
        [int]$ProxyPort=3128,

        # Param3 help description
        [Parameter(Mandatory=$false,
                   ParameterSetName='Parameter Set 1',
                   ValueFromPipelineByPropertyName=$false,
                   ValueFromPipeline=$false,
                   Position=2)]
        [string]$BypassProxy="*.ds.mfcr.cz;172.27.*;172.16.65.126;<local>",

        

        # Param4 help description
        [Parameter(ParameterSetName='Parameter Set 2')]
        [switch]$Disable,

        # Param5 help description
        [Parameter(ParameterSetName='Parameter Set 3')]
        [switch]$Enable
    )

    Begin
    {

    }
    
    Process
    {
        
        
        switch ($PsCmdlet.ParameterSetName) { 
            "Parameter Set 1"  {
                Write-Debug "Parameter Set 1";
                Write-Debug "parameter ProxyAddress=$ProxyAddress";
                Write-Debug "parameter ProxyPort=$ProxyPort";
                Write-Debug "parameter BypassProxy=$BypassProxy";
                Write-Debug "ProxyAddress:ProxyPort=$ProxyAddress`:$ProxyPort"

                Set-ItemProperty “HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings” -Name ProxyEnable -Value 1
                Set-ItemProperty “HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings” -Name ProxyServer -Value "$ProxyAddress`:$ProxyPort"
                Set-ItemProperty “HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings” -Name ProxyOverride -Value $BypassProxy
        

                break
            } 
            "Parameter Set 2"  {
                Write-Debug "Parameter Set 2";
                Write-Debug "parameter Disable=$Disable";
                if ($Disable.IsPresent -eq $true) {
                    Set-ItemProperty “HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings” -Name ProxyEnable -Value 0    
                }

                break
            }
            "Parameter Set 3"  {
                Write-Debug "Parameter Set 3";
                Write-Debug "parameter Enable=$Enable";
                if ($Enable.IsPresent -eq $true) {
                    Set-ItemProperty “HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings” -Name ProxyEnable -Value 1    
                }

                break
            }  
        } 

        
        
        
    }
    
    End
    {
    }
}

Tagged:

Leave a comment

To The Point

Anything about Technology and Business

PowerScripting Podcast

Shownotes and links for the PowerScripting Podcast, a podcast to help people learn Windows Powershell

Learn Powershell | Achieve More

What is this Powershell of which you speak?