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

如何在批处理中逐行输出每个用户组?

提取whoami /groups中的组名(批处理实现)

方法1:基础过滤提取(中文系统适用)

直接过滤冗余行并提取组名部分:

@echo off
for /f "tokens=2 delims=:" %%g in ('whoami /groups ^| findstr /v "Type SID Attributes ------"') do (
    echo %%g
)

方法2:多语言兼容版本

如果是英文系统或需要兼容中英文环境,用这个版本自动匹配对应标识:

@echo off
:: 检测系统语言对应的组名标识
whoami /groups | findstr /r "^组名:" > nul && (
    for /f "tokens=1* delims=:" %%a in ('whoami /groups ^| findstr "^组名:"') do echo %%b
) || (
    for /f "tokens=1* delims=:" %%a in ('whoami /groups ^| findstr "^Group name:"') do echo %%b
)

代码说明

  • findstr /v "Type SID Attributes ------":排除包含冗余字段名和分隔线的行
  • for /f "tokens=2 delims=:" %%g:以冒号为分隔符,提取每行冒号后的组名内容
  • 多语言版本通过findstr检测系统输出的标识文本,确保在中英文环境下都能正确提取

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 08:37:03