Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Win7 命令列一鍵關閉防火牆:netsh 指令適用腳本/批次自動化

編輯:Windows 7 系統介紹

需在腳本、批次檔、遠端管理中關閉 Windows 7 防火牆?使用 netsh advfirewall 指令可在命令列完整控制防火牆狀態,支援公用/私人/網域三種設定檔獨立操作。

關閉防火牆指令(需系統管理員權限)

  1. Win+R → 輸入 cmd → Ctrl+Shift+Enter (以系統管理員身分執行)。
  2. 執行以下指令關閉所有設定檔:
netsh advfirewall set allprofiles state off
  1. 若僅需關閉特定設定檔:
netsh advfirewall set publicprofile state off    # 公用網路
netsh advfirewall set privateprofile state off   # 私人/家庭/公司網路
netsh advfirewall set domainprofile state off    # 網域網路

CMD啟動

netsh指令執行

執行結果提示

防火牆狀態確認

管理員CMD執行

查詢目前狀態

netsh advfirewall show allprofiles

輸出中 State ON/OFF 顯示各設定檔啟用狀態。

開啟防火牆(恢復預設保護)

netsh advfirewall set allprofiles state on

舊版指令相容性說明

netsh firewall set opmode disable 為 Windows XP/Server 2003 時代指令,Win7 仍支援但會顯示「此指令已淘汰」警告,建議改用 netsh advfirewall。

批次檔範例 (toggle_fw.bat)

@echo off
netsh advfirewall show allprofiles | find "State" | find "ON" >nul
if %errorlevel% equ 0 (
    netsh advfirewall set allprofiles state off
    echo 防火牆已關閉
) else (
    netsh advfirewall set allprofiles state on
    echo 防火牆已開啟
)
pause

以系統管理員身分執行即可切換狀態。

Copyright © Windows教程網 All Rights Reserved