Skip to main content

How to Set or Remove Proxy Settings in Batch Script

Managing internet connectivity in an enterprise environment often requires switching between proxy configurations. Doing this manually through the Windows "Settings" GUI is slow and inconvenient for users who frequently move between home (direct) and office (proxy) networks. Programmatically adjusting these settings in a Batch script allows you to automate the "Switch" based on the network name or a simple user choice.

This guide will explain how to modify user-level proxy settings in the Registry and system-level settings using netsh.

1. Setting User-Level Proxy (Browser Settings)

Most desktop applications and browsers follow the settings stored in the Internet Settings registry key.

Script: Enable and Set a Proxy

@echo off
setlocal

set "reg=HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set "proxy_srv=192.168.1.100:8080"

echo [ACTION] Setting Proxy to %proxy_srv%...

:: 1. Enable the proxy
reg add "%reg%" /v "ProxyEnable" /t REG_DWORD /d 1 /f >nul

:: 2. Specify the server address
reg add "%reg%" /v "ProxyServer" /t REG_SZ /d "%proxy_srv%" /f >nul

if %errorlevel% equ 0 (
echo [SUCCESS] User proxy settings updated.
) else (
echo [ERROR] Failed to write proxy settings.
)

endlocal
pause

Script: Disable (Remove) the Proxy

@echo off
setlocal

set "reg=HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

echo [ACTION] Disabling Proxy...

reg add "%reg%" /v "ProxyEnable" /t REG_DWORD /d 0 /f >nul

if %errorlevel% equ 0 (
echo [SUCCESS] Proxy disabled. Connection is now direct.
) else (
echo [ERROR] Failed to update proxy settings.
)

endlocal
pause

2. Setting System-Level Proxy (WinHTTP)

Windows background services and Windows Update do not use the registry keys above. They use the WinHTTP subsystem.

@echo off
setlocal

:: Note: This requires Administrator privileges
set "myserver=proxy.company.com:8080"

echo [SYSTEM] Updating WinHTTP Proxy...

:: Set a new proxy
netsh winhttp set proxy "%myserver%"

if %errorlevel% equ 0 (
echo [SUCCESS] WinHTTP proxy set.
) else (
echo [ERROR] Failed. Are you running as Administrator?
)

:: To reset back to direct connection, use:
:: netsh winhttp reset proxy

endlocal
pause

3. Importing Settings from IE

A very common system administration trick is to set the user's proxy in the Internet Explorer GUI and then "Import" those settings into the system-wide WinHTTP configuration.

@echo off
setlocal

:: Note: This requires Administrator privileges
echo Syncing system proxy with user settings...
netsh winhttp import proxy source=ie

if %errorlevel% equ 0 (
echo [SUCCESS] WinHTTP proxy imported from IE settings.
) else (
echo [ERROR] Import failed. Are you running as Administrator?
)

endlocal
pause

How to Avoid Common Errors

Wrong Way: Forgetting the Port

If you set a proxy to 192.168.1.100 without the port (e.g., :8080), many applications will fail to connect because they will default to port 80, which the proxy server might not be listening on.

Correct Way: Always include the full IP/Domain and the Port: server:port.

Problem: Settings not applying immediately

Windows and browsers sometimes "Cache" the proxy settings. Simply changing a registry key doesn't always notify active applications of the change.

Solution: You can broadcast a settings-change notification to all running applications by calling the InternetSetOption API. This uses a small PowerShell snippet from your Batch script:

:: Notify running applications that proxy settings have changed
:: Option 39 = INTERNET_OPTION_SETTINGS_CHANGED
:: Option 37 = INTERNET_OPTION_REFRESH
powershell -Command "Add-Type -MemberDefinition '[DllImport(\"wininet.dll\")]public static extern bool InternetSetOption(IntPtr a,int b,IntPtr c,int d);' -Name WinInet -Namespace PInvoke; [PInvoke.WinInet]::InternetSetOption([IntPtr]::Zero,39,[IntPtr]::Zero,0); [PInvoke.WinInet]::InternetSetOption([IntPtr]::Zero,37,[IntPtr]::Zero,0)"
note

Most modern browsers will detect the registry change within a few seconds without this.

Best Practices and Rules

1. Administrative Privileges

Modifying the Registry keys in HKCU works for standard users. However, using the netsh winhttp commands requires Administrator rights. Without elevation, the system proxy will not be updated.

2. Bypass Lists (Exclusions)

If your company has internal sites (Intranet), ensure you don't break access to them by setting a proxy. Always include a "Proxy Override" list.

reg add "%reg%" /v "ProxyOverride" /t REG_SZ /d "localhost;127.0.0.1;<local>;*.mycompany.corp" /f

3. Verify the Change

After running your script, it's good practice to view the settings to ensure they were written correctly. reg query "%reg%" /v "ProxyEnable"

Conclusions

Automating proxy configuration in a Batch script is a powerful way to manage network mobility. By using Registry modifications for user apps and netsh for system services, you can create a single "Switch" that adapts your machine to any network environment. This efficiency reduces support tickets and ensures that your connectivity is always optimized for your current location.