Sunday, May 21, 2017

Keeping hashtable keys in order in PowerShell

These two expressions produce the same thing in PowerShell:

@{'A' = 'one'; 'B' = 'two'}
@{'B' = 'two'; 'A' = 'one'}

The A key always gets output first. If you want to keep them in the supplied order - say, if you're trying to get a CSV with columns arranged in a specific way - you can use [ordered]. This correctly keeps B first:

[ordered]@{'B' = 'two'; 'A' = 'one'}

No comments:

Post a Comment