powershell - 表格格式、列顺序

标签 powershell formatting

我需要决定表的列顺序。我的实际命令是:

$tab | Sort-Object "Pourcentage" -Descending |
  Format-Table -AutoSize |
  Out-String -Width 4000 |
  Out-File -Append O:\sigdci\public\parcoursArborescence\bilanAnalyse.txt

它给了我这个命令:

Derniere modification   Categorie recherchee   Dernier acces   Dossier   Pourcentage

但我需要将“Dossier”放在第一位,然后将“Categorie recherchee”和“Pourcentage”放在第二和第三。我该如何进行?

最佳答案

按所需顺序指定列标题:

$tab | Sort-Object "Pourcentage" -Descending | 
  Format-Table 'Dossier', 'Categorie recherchee', 'Pourcentage',
               'Derniere modification', 'Dernier acces' -AutoSize |
  Out-String -Width 4000 |
  Out-File -Append 'O:\sigdci\public\parcoursArborescence\bilanAnalyse.txt'

如果您需要动态确定列名称,您可以这样做:

$headers = $tab[0].PSObject.Properties |
           Where-Object MemberType -eq NoteProperty |
           Select-Object -Expand Name

但是,您必须以某种方式将该列表放入您想要的顺序。也许你可以这样做:

$allHeaders    = 'Dossier', 'Categorie recherchee', 'Pourcentage',
                 'Derniere modification', 'Dernier acces'
$actualHeaders = $tab[0].PSObject.Properties |
                 Where-Object { MemberType -eq NoteProperty } |
                 Select-Object -Expand Name
$headers = $allHeaders | Where-Object { $actualHeaders -contains $_ }

$allHeaders 是一个数组,其中包含按正确顺序排列的所有 header 。然后,从该列表中删除 $actualHeaders 中不存在的所有项目,并保留剩余 header 的顺序。

关于powershell - 表格格式、列顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34045436/

相关文章:

powershell - 在 Nano Server 上运行 Service Fabric

python - 使用 Python locals() 进行字符串格式化有缺点吗?

Java格式化风格

function - 如何在Powershell中返回202 Accepted然后继续处理请求

powershell - 在 Azure 中创建 VM 快照

powershell - 在Powershell中使用2个数组创建具有多个值的哈希

java - 如何将 LocalDate 格式化为用户位置的标准格式?

powershell - 使用-Co​​mObject Word.application而不安装Word

html - 在 HTML 中格式化圣经引述和/或圣经引用的最正确方法是什么?

html - 在 FireFox 中格式化好,在 IE 中格式化不好....帮助