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

PowerShell如何将带Index、Name属性的数组重排为带表头的规范表格

解决方案

如果你存储数组的变量已经是结构化对象(每个元素自带Index和Name属性),直接调用PowerShell内置的Format-Table命令即可生成你需要的带表头表格:

# 替换为你实际的数组变量名
$你的数组变量 | Format-Table Index, Name -AutoSize

如果你的原始内容是未结构化的多行字符串,需要先将文本转换为结构化对象再输出表格,完整处理代码如下:

# 示例:将原始文本内容存入$rawContent变量,替换为你实际的内容来源
$rawContent = @'
Index : 1
Name : Windows 10 Education
Index : 2
Name : Windows 10 Education N
Index : 3
Name : Windows 10 Enterprise
Index : 4
Name : Windows 10 Enterprise N
Index : 5
Name : Windows 10 Pro
Index : 6
Name : Windows 10 Pro N
Index : 7
Name : Windows 10 Pro Education
Index : 8
Name : Windows 10 Pro Education N
Index : 9
Name : Windows 10 Pro for Workstations
Index : 10
Name : Windows 10 Pro N for Workstations
'@

# 解析文本生成结构化对象数组
$win10Versions = $rawContent -split "`n" | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne '' } | ForEach-Object {
    $lineParts = $_ -split '\s+:\s+', 2
    if ($lineParts[0] -eq 'Index') {
        $currentObj = [PSCustomObject]@{
            Index = [int]$lineParts[1]
            Name  = $null
        }
    }
    else {
        $currentObj.Name = $lineParts[1]
        $currentObj
    }
}

# 输出规范表格
$win10Versions | Format-Table Index, Name -AutoSize

如果需要将生成的表格保存到本地文件,在输出命令后追加Out-File即可:

$win10Versions | Format-Table Index, Name -AutoSize | Out-File -FilePath "Win10版本列表.txt" -Encoding UTF8

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 03:15:03