Skip to content

Instantly share code, notes, and snippets.

@meistermeier
Created August 28, 2024 07:38
Show Gist options
  • Save meistermeier/df1aec33c53b5b03a26b7cd842faf708 to your computer and use it in GitHub Desktop.
Save meistermeier/df1aec33c53b5b03a26b7cd842faf708 to your computer and use it in GitHub Desktop.
Changing the proxy in Windows via PowerShell script. I ran through the pain so you don't have to. Could it be nicer? For sure! Do I want to spend more time on this than needed? Hell no!
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$server = "<your_host>"
$port = "<your_port>"
# Set the proxy without any check (for configuring it before entering the network)
function Set-Proxy-No-Check {
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyServer -Value "$($server):$($port)"
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyEnable -Value 1
}
# Set the proxy with a check (needs to be already in the same network)
function Set-Proxy {
#Test if the TCP Port on the server is open before applying the settings
If ((Test-NetConnection -ComputerName $server -Port $port).TcpTestSucceeded) {
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyServer -Value "$($server):$($port)"
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyEnable -Value 1
}
Else {
Write-Error -Message "The proxy address is not valid: $($server):$($port)"
}
}
function Remove-Proxy (){
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyServer -Value ""
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyEnable -Value 0
}
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Proxy'
$form.Size = New-Object System.Drawing.Size(200,200)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(25,120)
$OKButton.Size = New-Object System.Drawing.Size(75,25)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(100,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,25)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Height = 80
$listBox.Width = 200
[void] $listBox.Items.Add('Proxy need')
[void] $listBox.Items.Add('Proxy need no check')
[void] $listBox.Items.Add('Home')
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItem
If ($x -eq "Proxy need") {
Set-Proxy
}
ElseIf ($x -eq "Proxy need no check") {
Set-Proxy-No-Check
}
Else {
Remove-Proxy
}
}
@meistermeier
Copy link
Author

This is mostly a patchwork of several sources from the internet. So thanks to everyone who finds herself/himself here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment