2

I need to decide the columns' orders of my table. My actual command is that one:

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

It gives me that order:

Derniere modification   Categorie recherchee   Dernier acces   Dossier   Pourcentage

But I need "Dossier" to be first, then "Categorie recherchee" and "Pourcentage" shall be 2nd and 3rd. How shall I proceed?

1 Answer 1

2

Specify the column headers in the desired order:

$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'

If you need to dynamically determine the column names you could do it like this:

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

However, you'd have to bring that list into your desired order somehow. Perhaps you could do it like this:

$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 is an array that contains all headers in the correct order. Then you remove all items that aren't present in $actualHeaders from that list, preserving the order of the remaining headers.

Sign up to request clarification or add additional context in comments.

3 Comments

Will this work even when one of the columns doesnt exist ? Because the program's user is able to activate some columns only.
Problem is that when all columns are shown, they are taking too much place. Do you have an idea for not showing the empty ones ?
+1 for showing how to do this in a more dynamic fashion rather than just hard-coding like all the other examples i have seen! This was really helpful for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.