Share via


Enable Disable Proxy Settings via PowerShell.

Introduction

Let us discuss on building a simple cmdlet function to SET the proxy server's address and port with optional parameter to set the Automatic Configuration Script in PowerShell
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.

Parameters

"proxy" is the mandatory parameter for the function,
"acs" is optional while calling the function.

CmdLet

Set-NetProxy and Disable-NetProxy
This function will edit the values at "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" related to the Proxy.
The function would  enable the Proxy, if disabled by default.

Caution: Editing the registry is risky! Always backup, test thoroughly, and execute.

Syntax

Enable proxy:
    Set-NetProxy [-Proxy] <string[]> [[-acs] <string[]>]  [<CommonParameters>]

Disable-proxy:
    Disable-NetProxy 

Script

Enable-NetProxy

01.<#
02. .Synopsis
03. This function will set the proxy settings provided as input to the cmdlet.
04. .Description
05. This function will set the proxy server and (optional) Automatic configuration script.
06. .Parameter Proxy Server
07. This parameter is set as the proxy for the system.
08. Data from. This parameter is Mandatory.
09. .Example
10. Setting proxy information.
11. Set-NetProxy -proxy "proxy:7890"
12. .Example
13. Setting proxy information and (optional) Automatic Configuration Script.
14. Set-NetProxy -proxy "proxy:7890" -acs "http://proxy:7892"
15.#>
16. 
17. 
18.Function Set-NetProxy
19.    {
20.        [CmdletBinding()]
21.        Param(
22.            
23.            [Parameter(Mandatory=$True,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
24.            [String[]]$Proxy,
25. 
26.            [Parameter(Mandatory=$False,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
27.            [AllowEmptyString()]
28.            [String[]]$acs
29.                    
30.        )
31. 
32.        Begin
33.        {
34. 
35.                $regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
36.            
37.        }
38.        
39.        Process
40.        {
41.            
42.            Set-ItemProperty -path $regKey ProxyEnable -value 1
43. 
44.            Set-ItemProperty -path $regKey ProxyServer -value $proxy
45.                                
46.            if($acs)
47.            {          
48.                
49.                     Set-ItemProperty -path $regKey AutoConfigURL -Value $acs        
50.            }
51. 
52.        }
53.        
54.        End
55.        {
56. 
57.            Write-Output "Proxy is now enabled"
58. 
59.            Write-Output "Proxy Server : $proxy"
60. 
61.            if ($acs)
62.            {
63.                
64.                Write-Output "Automatic Configuration Script : $acs"
65. 
66.            }
67.            else
68.            {
69.                
70.                Write-Output "Automatic Configuration Script : Not Defined"
71. 
72.            }
73.        }
74.    }

Disable-NetProxy

01.Function Disable-NetProxy
02.{
03.  Begin
04.    {
05. 
06.            $regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
07.        
08.    }
09.    
10.    Process
11.    {
12.        
13.        Set-ItemProperty -path $regKey ProxyEnable -value 0 -ErrorAction Stop
14. 
15.        Set-ItemProperty -path $regKey ProxyServer -value "" -ErrorAction Stop
16.                            
17.        Set-ItemProperty -path $regKey AutoConfigURL -Value "" -ErrorAction Stop        
18.       
19.    }
20.    
21.    End
22.    {
23. 
24.        Write-Output "Proxy is now Disabled"
25. 
26.              
27.    }
28. 
29.}

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

Conclusion

Yes, there are so many other ways to achieve this and this is one of the way.