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

PowerShell ForEach循环输出重复表头问题解决求助

解决PowerShell ForEach循环中Format-Table表头重复的问题

问题出在你把Format-Table(简称ft)放在了循环内部——每次循环执行Get-ADComputer后,都会单独生成一个带表头的表格输出,自然就会重复显示表头。

修改方案1:先收集所有结果再统一格式化

$list = Get-Content C:\Temp\Description\Servers.txt
# 循环收集所有服务器的查询结果
$serverResults = foreach ($server in $list) {
    # 只加载需要的description属性,比-properties *效率更高
    Get-ADComputer -Identity $server -Properties description | Select-Object name, description
}
# 统一格式化输出,只显示一次表头
$serverResults | Format-Table -AutoSize

修改方案2:直接将循环输出管道到Format-Table

$list = Get-Content C:\Temp\Description\Servers.txt
foreach ($server in $list) {
    Get-ADComputer -Identity $server -Properties description | Select-Object name, description
} | Format-Table -AutoSize

两种方式都是让所有查询结果先汇总,再一次性进行表格格式化,这样就只会显示一次表头,符合你想要的输出效果。

内容的提问来源于stack exchange,提问作者night-fury

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 15:40:28