Tuesday 4 July 2017

Enable/Disable Proxy Settings via PowerShell.

Hello,


Here is a PowerShell function to SET the HTTP proxy server's address and port with optional parameter to set the Automatic Configuration Script.
The input of the CmdLet has two input parameters -proxy which would set the proxy server details and -acs for the Automatic Configuration Script value.
-proxy is the mandatory parameter and -acs is optinal while calling the function.

Set-InternetProxy

This function will edit the values at "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" as below.
It enables the Proxy if disabled by default.
  
SYNTAX
Enable proxy:
    Set-InternetProxy [-Proxy] <string[]> [[-acs] <string[]>]  [<CommonParameters>]

 Disable-proxy:
    Disable-InternetProxy






Here is the function's code :

Set-InternetProxy

<#
.Synopsis
This function will set the proxy settings provided as input to the cmdlet.
.Description
This function will set the proxy server and (optinal) Automatic configuration script.
.Parameter ProxyServer
This parameter is set as the proxy for the system.
Data from. This parameter is Mandatory
.Example
Setting proxy information
Set-InternetProxy -proxy "proxy:7890"
.Example
Setting proxy information and (optinal) Automatic Configuration Script
Set-InternetProxy -proxy "proxy:7890" -acs "http://proxy:7892"
#>


Function Set-InternetProxy
{
    [CmdletBinding()]
    Param(
       
        [Parameter(Mandatory=$True,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [String[]]$Proxy,

        [Parameter(Mandatory=$False,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [AllowEmptyString()]
        [String[]]$acs
               
    )

    Begin
    {

            $regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
       
    }
   
    Process
    {
       
        Set-ItemProperty -path $regKey ProxyEnable -value 1

        Set-ItemProperty -path $regKey ProxyServer -value $proxy
                           
        if($acs)
        {           
           
                 Set-ItemProperty -path $regKey AutoConfigURL -Value $acs         
        }

    }
   
    End
    {

        Write-Output "Proxy is now enabled"

        Write-Output "Proxy Server : $proxy"

        if ($acs)
        {
           
            Write-Output "Automatic Configuration Script : $acs"

        }
        else
        {
           
            Write-Output "Automatic Configuration Script : Not Defined"

        }
    }
}


Disable-InternetProxy 

Function Disable-InternetProxy
{
  Begin
    {

            $regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
       
    }
   
    Process
    {
       
        Set-ItemProperty -path $regKey ProxyEnable -value 0 -ErrorAction Stop

        Set-ItemProperty -path $regKey ProxyServer -value "" -ErrorAction Stop
                           
        Set-ItemProperty -path $regKey AutoConfigURL -Value "" -ErrorAction Stop         
      
    }
   
    End
    {

        Write-Output "Proxy is now Disabled"

             
    }

}


Note: For the changes to take effect the IE (Internet Explorer) should be opened and closed.

Happy Scripting!

1 comment:

  1. If your looking for Online Illinois license plate sticker renewals then you have need to come to the right place.We offer the fastest Illinois license plate sticker renewals in the state. vpn free

    ReplyDelete