Windows 防火牆透過 GUI 設定雖直覺,但批次管理、遠端配置、腳本自動化時,netsh advfirewall 指令更具彈性與效率。本文整理常用查詢與設定指令,幫助管理員快速掌握防火牆狀態、規則、日誌等關鍵配置。
# 進入 netsh 防火牆語境
netsh advfirewall
# 顯示目前設定檔(Domain/Private/Public)狀態
show currentprofile
# 顯示所有防火牆規則(含啟用/停用)
show rule name=all
# 顯示特定規則詳細資訊
show rule name="允許的程式"
# 顯示允許的程式清單
show allowedprogram
# 顯示開放連接埠
show portopening
# 顯示 ICMP 設定
show icmpsetting
# 顯示日誌設定
show logging
# 顯示通知設定
show notifications
# 顯示作業模式(開啟/關閉/封鎖所有)
show opmode
# 顯示服務設定
show service
# 顯示多播/廣播回應
show multicastbroadcastresponse
# 顯示完整狀態摘要
show state
# 啟用/停用防火牆(目前設定檔)
set currentprofile state on|off
# 設定預設輸入/輸出動作
set currentprofile firewallpolicy blockinbound,allowoutbound
# 新增/修改允許的程式規則
set allowedprogram program="C:\Path\App.exe" name="MyApp" mode=enable|disable profile=all
# 開放/關閉連接埠
set portopening protocol=TCP port=8080 name="WebPort" mode=enable profile=all
# 設定 ICMP 允許類型(如允許 Ping 回應)
set icmpsetting type=8 mode=enable # Echo Request
# 設定日誌路徑、大小、記錄類型
set logging filename="%systemroot%\system32\LogFiles\Firewall\pfirewall.log" maxfilesize=4096 droppedconnections=enable connections=enable
# 設定通知(彈出提示)
set notifications enable|disable
# 設定服務(如遠端桌面)
set service remotedesktop mode=enable profile=domain
.cmd 或 .ps1 批次部署多台主機。netsh -r RemotePC -u Domain\Admin -p Password advfirewall show allprofilesnetsh advfirewall export "fwpolicy.wfw" / import "fwpolicy.wfw"netsh advfirewall reset# 同等功能,物件導向更強大
Get-NetFirewallProfile
Get-NetFirewallRule
New-NetFirewallRule -DisplayName "Allow HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow
Set-NetFirewallProfile -DefaultInboundAction Block
善用 netsh 或 PowerShell 將防火牆配置納入版本控管與自動化部署流程,確保環境一致性並降低人為錯誤風險。