You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PowerShell将符合条件的Windows Defender防火墙规则输出为类Excel表格的网格格式

如何通过PowerShell将符合条件的Windows Defender防火墙规则输出为类Excel表格的网格格式

看起来你已经搞定了筛选Windows Defender防火墙规则的核心逻辑,就差把输出改成类似Excel的网格格式了对吧?这里有几个简单实用的方案,按需选择就行:

1. 用PowerShell自带的交互式网格窗口(最快体验)

PowerShell内置的Out-GridView命令可以直接弹出一个和Excel表格风格高度相似的交互式窗口,支持排序、筛选、搜索,还能直接复制内容到Excel里。你只需要在现有脚本的末尾加上这个命令就行:

Get-NetFirewallRule |
Where-Object {($_.Profiles -band [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Profile]::Domain) -or ($_.Profiles -eq [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Profile]::Any)} |
ForEach-Object {
    $portFilter = Get-NetFirewallPortFilter -AssociatedNetFirewallRule $_
    $_ | Select-Object -Property `
        Name,
        DisplayName,
        DisplayGroup,
        Profiles,
        Direction,
        @{n='Protocol'; e={$portFilter.Protocol}},
        @{n='LocalPort'; e={$portFilter.LocalPort}},
        @{n='RemotePort'; e={$portFilter.RemotePort}},
        @{n='RemoteAddress'; e={(Get-NetFirewallAddressFilter -AssociatedNetFirewallRule $_).RemoteAddress}},
        Enabled,
        Profile,
        Action
} | Out-GridView -Title "符合条件的防火墙规则"

我给加了-Title参数用来设置窗口标题,你可以改成自己需要的文字。这个窗口完全是交互式的,操作体验和Excel表格很接近,选中行后直接复制就能粘贴到Excel里使用。

2. 导出到可直接用Excel打开的文件

如果需要生成可以保存的文件,方便后续查看或分享,有两种方式:

方法一:用原生命令生成CSV文件(无需额外工具)

CSV是逗号分隔的文本文件,Excel可以完美识别成标准表格,而且不需要安装任何额外工具。只需要把Out-GridView换成Export-Csv,指定输出路径即可:

Get-NetFirewallRule |
Where-Object {($_.Profiles -band [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Profile]::Domain) -or ($_.Profiles -eq [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Profile]::Any)} |
ForEach-Object {
    $portFilter = Get-NetFirewallPortFilter -AssociatedNetFirewallRule $_
    $_ | Select-Object -Property `
        Name,
        DisplayName,
        DisplayGroup,
        Profiles,
        Direction,
        @{n='Protocol'; e={$portFilter.Protocol}},
        @{n='LocalPort'; e={$portFilter.LocalPort}},
        @{n='RemotePort'; e={$portFilter.RemotePort}},
        @{n='RemoteAddress'; e={(Get-NetFirewallAddressFilter -AssociatedNetFirewallRule $_).RemoteAddress}},
        Enabled,
        Profile,
        Action
} | Export-Csv -Path "C:\temp\FirewallRules.csv" -NoTypeInformation -Encoding UTF8

参数说明:

  • -NoTypeInformation:去掉CSV文件开头的类型注释,让Excel打开后更干净
  • -Encoding UTF8:避免中文名称出现乱码问题(如果你的防火墙规则有中文名称的话)

生成后直接双击CSV文件,就能用Excel打开,显示为标准的表格格式。

方法二:生成真正的XLSX格式文件(专业Excel体验)

如果需要直接生成Excel专属的XLSX格式文件(不是CSV),可以用社区维护的ImportExcel模块,这个模块专门用来处理Excel文件,功能非常强大。

首先需要安装模块(请用管理员权限打开PowerShell执行):

Install-Module -Name ImportExcel -Scope CurrentUser -Force

然后修改脚本用Export-Excel命令导出:

Get-NetFirewallRule |
Where-Object {($_.Profiles -band [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Profile]::Domain) -or ($_.Profiles -eq [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Profile]::Any)} |
ForEach-Object {
    $portFilter = Get-NetFirewallPortFilter -AssociatedNetFirewallRule $_
    $_ | Select-Object -Property `
        Name,
        DisplayName,
        DisplayGroup,
        Profiles,
        Direction,
        @{n='Protocol'; e={$portFilter.Protocol}},
        @{n='LocalPort'; e={$portFilter.LocalPort}},
        @{n='RemotePort'; e={$portFilter.RemotePort}},
        @{n='RemoteAddress'; e={(Get-NetFirewallAddressFilter -AssociatedNetFirewallRule $_).RemoteAddress}},
        Enabled,
        Profile,
        Action
} | Export-Excel -Path "C:\temp\FirewallRules.xlsx" -AutoSize -BoldTopRow

参数说明:

  • -AutoSize:自动调整所有列的宽度,适配内容
  • -BoldTopRow:把表头行设置为加粗样式,完全符合Excel的常用格式

生成的XLSX文件打开后就是现成的规范表格,不需要再调整格式。

备注:内容来源于stack exchange,提问作者zerokool000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 07:33:16