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

如何使用PowerShell检索本地组策略并过滤已应用GPO列表

检索本地已应用组策略对象列表的方法

如果你只想从gpresult的输出中提取**已应用组策略对象(Applied Group Policy Objects)**的列表,可以通过以下两种方式实现:

1. 命令提示符(CMD)方式

使用findstr和简单脚本逻辑定位目标段落:

@echo off
:: 获取"Applied Group Policy Objects"所在行号
gpresult /r | findstr /n "Applied Group Policy Objects" > temp.txt
set /p line=<temp.txt
for /f "delims=:" %%a in ("%line%") do set startline=%%a

:: 提取目标段落(从起始行到下一个小节前)
gpresult /r | more +%startline% | findstr /v "The following GPOs were not applied"

:: 清理临时文件
del temp.txt

执行这段脚本后,只会输出已应用的GPO列表,其他无关内容会被过滤掉。

2. PowerShell方式

利用PowerShell的文本处理能力更精准地提取内容:

# 获取gpresult的完整输出
$gpOutput = gpresult /r

# 定位目标段落的起始和结束行号
$startLine = ($gpOutput | Select-String "Applied Group Policy Objects").LineNumber
$endLine = ($gpOutput | Select-String "The following GPOs were not applied").LineNumber

# 提取并输出已应用GPO列表
$gpOutput[$startLine..($endLine - 2)]

如果只想查看计算机或用户范围的已应用GPO,可以在gpresult命令后添加/scope computer或/scope user参数,比如:

$gpOutput = gpresult /r /scope computer

补充说明

  • 如果本地组策略没有应用任何GPO,输出会显示N/A;
  • 运行这些命令需要管理员权限,否则可能无法获取完整的组策略信息。

内容的提问来源于stack exchange,提问作者user18970328

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 20:35:25