如何使用 PowerShell 管理 Windows 防火牆規則
![如何使用 PowerShell 管理 Windows 防火牆規則](https://cdn.thewindowsclub.blog/wp-content/uploads/2023/09/manage-windows-firewall-rules-with-powershell-1-640x375.webp)
Windows Defender 防火牆預裝在所有 Windows 計算機上。您可以在 Windows 防火牆中創建用於不同目的的規則,例如阻止特定應用程序訪問 Internet。本文介紹如何使用 PowerShell 管理 Windows 防火牆規則。
![使用 PowerShell 管理 Windows 防火牆規則 使用 PowerShell 管理 Windows 防火牆規則](https://cdn.thewindowsclub.blog/wp-content/uploads/2023/09/manage-windows-firewall-rules-with-powershell.webp)
如何使用 PowerShell 管理 Windows 防火牆規則
您可以通過 Windows Defender 防火牆和高級安全 UI輕鬆配置 Windows 防火牆規則。您可以通過控制面板啟動它。現在,您還可以使用 PowerShell 管理 Windows 防火牆規則。在本文中,我們將向您展示如何做到這一點。
要通過 PowerShell 管理 Windows 防火牆中的規則,您必須使用NetFirewallRule cmdlet,它是 NetSecurity 模塊的一部分。您可以通過在 Windows PowerShell 中執行以下命令來查看所有 NetSecurity cmdlet:
Get-Command -Module NetSecurity
![所有 NetSecurity cmdlet 所有 NetSecurity cmdlet](https://cdn.thewindowsclub.blog/wp-content/uploads/2023/09/all-netsecurity-cmdlets.webp)
Windows PowerShell 具有以下三種類型的配置文件:
- 域名簡介
- 私人檔案
- 公開資料
![Windows 防火牆配置文件 Windows 防火牆配置文件](https://cdn.thewindowsclub.blog/wp-content/uploads/2023/09/windows-firewall-profiles.webp)
默認情況下,Windows 防火牆對於所有這三個配置文件均保持打開狀態。您可以通過控制面板打開具有高級安全 UI 的 Windows Defender 防火牆或在運行命令框中使用以下命令來查看此內容:
wf.msc
請注意,您必須以管理員身份啟動 Windows PowerShell,否則命令將無法執行,並且您將在 PowerShell 中收到錯誤消息。
如果要查看特定 Windows 防火牆配置文件的設置,請使用以下命令:
Get-NetFirewallProfile -Name <name of the profile>
![查看 Windows 防火牆配置文件設置 查看 Windows 防火牆配置文件設置](https://cdn.thewindowsclub.blog/wp-content/uploads/2023/09/view-windows-firewall-profile-settings.webp)
在上面的命令中,正確輸入配置文件的名稱。例如,如果要查看 Windows Defender 防火牆中域配置文件的設置,命令將為:
Get-NetFirewallProfile -Name Domain
使用 PowerShell 啟用或禁用 Windows 防火牆
讓我們看看如何使用 PowerShell 啟用或禁用 Windows 防火牆。如果要禁用所有配置文件的 Windows 防火牆,請使用以下命令:
Set-NetFirewallProfile -All -Enabled False
如果要為特定配置文件啟用或禁用 Windows 防火牆,則必須將上述命令中的 All 替換為該配置文件名稱。
![禁用 Windows 防火牆公共配置文件 PowerShell 禁用 Windows 防火牆公共配置文件 PowerShell](https://cdn.thewindowsclub.blog/wp-content/uploads/2023/09/disable-windows-firewall-public-profile-powershell.webp)
例如,如果您想禁用公共配置文件的 Window 防火牆,命令將為:
Set-NetFirewallProfile -Profile Public -Enabled False
![檢查 Windows 防火牆配置文件狀態 檢查 Windows 防火牆配置文件狀態](https://cdn.thewindowsclub.blog/wp-content/uploads/2023/09/check-windows-firewall-profile-status.webp)
您可以使用以下命令檢查 Windows 防火牆配置文件的狀態:
Get-NetFirewallProfile | Format-Table Name, Enabled
正如您在上面的屏幕截圖中看到的,PowerShell 將 Windows 防火牆公共配置文件的狀態顯示為 False,這意味著該配置文件的 Windows 防火牆已禁用。
![禁用 Windows 防火牆公共配置文件 禁用 Windows 防火牆公共配置文件](https://cdn.thewindowsclub.blog/wp-content/uploads/2023/09/disable-windows-firewall-public-profile.webp)
您還可以在具有高級安全 UI 的 Windows Defender 防火牆中檢查相同內容。如果要啟用禁用的配置文件,則必須使用 True 代替 False。例如,這裡我們禁用了 Windows 防火牆中的公共配置文件。現在,要重新啟用它,命令是:
Set-NetFirewallProfile -Profile Public -Enabled True
如果您已禁用所有 Windows Defender 配置文件並且想要再次啟用所有這些配置文件,請使用以下命令:
Set-NetFirewallProfile -All -Enabled True
使用 PowerShell 創建和管理 Windows 防火牆規則
現在,讓我們看看如何使用 PowerShell 創建和管理 Windows 防火牆規則。如果您使用第三方防病毒軟件並且您的防火牆由該防病毒軟件管理,則 PowerShell 命令將不起作用。您將能夠成功創建規則,但如果您的防火牆由第三方防病毒軟件管理,這些規則將不起作用。
如果要創建新的 Windows 防火牆規則,則必須使用以下 cmdlet:
New-NetFirewallRule
假設您想阻止某個程序通過您的 WiFi 配置文件訪問互聯網;使用以下命令:
New-NetFirewallRule -Program "program path" -Action Block -Profile <profile name> -DisplayName "write display name here" -Description "write description here" -Direction Outbound
上述命令將為 Windows Defender 防火牆中所需的程序創建出站規則。在上述命令中,將程序路徑替換為正確的程序路徑,並將配置文件名稱替換為正確的 Windows 防火牆配置文件。顯示名稱是防火牆規則的名稱,描述是可選的。
![使用 PowerShell 阻止程序 Windows 防火牆 使用 PowerShell 阻止程序 Windows 防火牆](https://cdn.thewindowsclub.blog/wp-content/uploads/2023/09/block-program-windows-firewall-with-powershell.webp)
例如,如果您想阻止 Chrome 瀏覽器訪問私人配置文件,則命令將為:
New-NetFirewallRule -Program "C:\Program Files\Google\Chrome\Application\chrome.exe" -Action Block -Profile Public -DisplayName "Block Chrome browser" -Description "Chrome browser blocked" -Direction Outbound
如果您不想向防火牆規則添加說明,可以從上述命令中刪除 -Description“Chrome 瀏覽器被阻止”部分。上述命令僅適用於公共配置文件。因此,如果您的網絡連接配置文件不是公共的,則此命令將不起作用。您可以在 Windows 設置中查看 WiFi 連接的配置文件。以下步驟將幫助您:
![查看網絡配置文件類型 查看網絡配置文件類型](https://cdn.thewindowsclub.blog/wp-content/uploads/2023/09/view-network-profile-type.webp)
- 打開 Windows 11/10 設置。
- 轉至網絡和互聯網 > Wi-Fi。
- 選擇您的互聯網連接。
- 展開網絡連接屬性選項卡。
您將在那裡看到網絡配置文件類型。
如果您想要阻止某個程序,例如針對所有網絡配置文件使用 Google Chrome,請鍵入所有配置文件名稱,並以逗號分隔。因此,命令將是:
New-NetFirewallRule -Program "C:\Program Files\Google\Chrome\Application\chrome.exe" -Action Block -Profile Domain, Private, Public -DisplayName "Block Chrome browser" -Description "Chrome browser blocked" -Direction Outbound
請注意,您必須按照上述命令中寫入的確切順序鍵入配置文件名稱,即“域”、“私有”、“公共”。否則,您將收到錯誤。
同樣,您可以使用 PowerShell 在 Windows Defender 防火牆中創建規則來阻止特定網站。但為此,您應該知道該特定網站的 IP 地址。您可以使用以下命令獲取網站的IP地址:
nslookup <website name>
如果您要阻止的網站顯示多個 IP 地址,則必須寫下所有這些 IP 地址。用逗號分隔所有 IP 地址。
用於在 Windows Defender 防火牆中創建阻止網站的規則的命令是:
New-NetFirewallRule -DisplayName "Block Website"-Description "Website Blocked"-Direction Outbound –LocalPort Any -Protocol Any -Action Block -RemoteAddress IP1, IP2
上面的示例顯示瞭如何用逗號分隔特定網站的 IP 地址。
使用 Windows PowerShell 啟用、禁用和刪除防火牆規則
如果要啟用、禁用或刪除防火牆規則,必須在 PowerShell 中使用以下 cmdlet:
Enable-NetFirewallRule
Disable-NetFirewallRule
Remove-NetFirewallRule
![刪除防火牆規則 PowerShell 刪除防火牆規則 PowerShell](https://cdn.thewindowsclub.blog/wp-content/uploads/2023/09/delete-a-firewall-rule-powershell.webp)
在上述每個 cmdlet 中,您必須輸入防火牆規則的正確名稱。假設您已經創建了一條名為 Block Chrome 的防火牆規則,現在您想要刪除它,那麼命令將是:
Remove-NetFirewallRule -DisplayName 'Block Chrome'
如何在 PowerShell 中查看 Windows 防火牆規則?
如果您想查看您在 Windows 防火牆中創建的出站阻止規則,您必須使用以下命令:
Get-NetFirewallRule -Action Block -Enabled True -Direction Outbound
![查看 Windows 防火牆規則 PowerShell 查看 Windows 防火牆規則 PowerShell](https://cdn.thewindowsclub.blog/wp-content/uploads/2023/09/view-windows-firewall-rules-powershell.webp)
上述命令將僅列出活動的防火牆規則。如果要查看禁用的防火牆規則,請將上述命令中的 True 替換為 False。
就是這樣。我希望這有幫助。
如何在 PowerSell 中設置防火牆規則?
您可以使用不同的 NetFirewallRule cmdlet 在 PowerShell 中設置防火牆規則。例如,如果要創建新的防火牆規則,則必須使用 New-NetFirewallRule cmdlet。
發佈留言