我需要决定表的列顺序。我的实际命令是:
$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/