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

如何自定义PowerShell命令Get-WindowsFeature的Path输出格式

实现方案

核心思路

  • 预存所有Windows功能的路径与对应显示名、标识的映射关系,避免重复查询
  • Path属性是Windows功能的唯一层级标识,不会出现重复匹配问题
  • 拆分每个功能的反斜杠分隔路径,逐级匹配映射表获取对应信息
  • 按指定格式拼接每一级信息,最终得到目标输出

完整实现代码

# 构建路径映射表,Key为功能路径,Value为「显示名 [标识]」格式字符串
$featurePathMap = @{}
Get-WindowsFeature | ForEach-Object {
    $featurePathMap[$_.Path] = "$($_.DisplayName) [$($_.Name)]"
}

# 遍历所有功能处理路径输出
Get-WindowsFeature | ForEach-Object {
    $pathSegments = $_.Path -split '\\'
    $currentPath = ''
    $resultSegments = @()
    foreach ($seg in $pathSegments) {
        $currentPath = if ($currentPath) { "$currentPath\$seg" } else { $seg }
        $resultSegments += $featurePathMap[$currentPath]
    }
    # 用分号拼接输出
    $resultSegments -join ';'
}

输出示例

针对示例路径Services de fichiers et de stockage\Services de fichiers et iSCSI,处理后输出格式如下:

Services de fichiers et de stockage [File-Services];Services de fichiers et iSCSI [FS-FileServer]

可选调整项

  • 如需筛选特定路径的结果,可在第二个Get-WindowsFeature后添加过滤规则,示例:Get-WindowsFeature | Where-Object { $_.Path -match 'Fichiers' }
  • 如需导出结果到本地文件,可在代码末尾追加 | Out-File -FilePath C:\windows_features_output.txt -Encoding UTF8

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 12:27:03