Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Windows 綜合教學 >> 關於 Windows

Windows 7 一鍵切換 IP 設定:批次檔快速切換 DHCP 與靜態 IP

編輯:關於 Windows

筆電在公司、家中、客戶端等不同網路環境移動,頻繁手動切換 IP 設定 (DHCP/靜態 IP) 十分繁瑣。撰寫兩個批次檔 (.bat),分別對應「自動取得 IP (DHCP)」與「指定靜態 IP」,雙擊即可一鍵切換,大幅提升效率。

批次檔內容

DHCP.bat (自動取得 IP)

@echo off
netsh interface ip set address "區域連線" source=dhcp
netsh interface ip set dns "區域連線" source=dhcp
pause

Static_IP.bat (指定靜態 IP,請依實際環境修改參數)

@echo off
netsh interface ip set address "區域連線" static 10.0.0.123 255.255.255.0 10.0.0.1 1
netsh interface ip set dns "區域連線" static 218.30.19.40
netsh interface ip add dns "區域連線" 211.11.1.67 index=2
pause

參數說明:
"區域連線" 為網路介面卡名稱 (可用 netsh interface show interface 查看,常見為「乙太網路」、「Wi-Fi」、「區域連線」)。
static IP 子網路遮罩 閘道 跳躍點、static DNS、add dns index=2 為備用 DNS。

使用方法

  1. 記事本建立上述兩個檔案,分別儲存為 DHCP.bat、Static_IP.bat。
  2. 滑鼠右鍵檔案 → 「建立捷徑」 → 將捷徑放置桌面或釘選到工作列/開始選單。
  3. 切換網路環境時,以系統管理員身分執行對應捷徑 (右鍵捷徑 → 「以系統管理員身分執行」),或設定捷徑屬性 → 「進階」→ 勾選「以系統管理員身分執行」。

進階:單一檔案選單式切換

@echo off
echo 1. 切換至 DHCP
echo 2. 切換至靜態 IP (公司)
echo 3. 切換至靜態 IP (家裡)
set /p choice=請輸入選項:
if "%choice%"=="1" goto dhcp
if "%choice%"=="2" goto static1
if "%choice%"=="3" goto static2
goto end

:dhcp
netsh interface ip set address "區域連線" source=dhcp
netsh interface ip set dns "區域連線" source=dhcp
goto end

:static1
netsh interface ip set address "區域連線" static 10.0.0.123 255.255.255.0 10.0.0.1 1
netsh interface ip set dns "區域連線" static 218.30.19.40
netsh interface ip add dns "區域連線" 211.11.1.67 index=2
goto end

:static2
netsh interface ip set address "區域連線" static 192.168.1.100 255.255.255.0 192.168.1.1 1
netsh interface ip set dns "區域連線" static 168.95.1.1
netsh interface ip add dns "區域連線" 8.8.8.8 index=2
goto end

:end
pause

注意事項

  • 需系統管理員權限執行 netsh 指令。
  • 介面卡名稱含空格時必須用雙引號括住。
  • 切換後可用 ipconfig 驗證 IP 設定是否生效。
  • Windows 10/11 同樣支援 netsh 指令,批次檔通用。

善用批次檔一鍵切換網路設定,告別繁瑣的 GUI 點點點。

Copyright © Windows教程網 All Rights Reserved