Can I force input key names remain as such, rather than become values of key
and value
? If so, how?
#151
-
ExamplesGet-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' | Format-List The aforementioned command outputs the undermentioned data: SystemRoot : C:\WINDOWS
BuildBranch : ge_release
BuildLab : 26100.ge_release.240331-1435
BuildLabEx : 26100.1.amd64fre.ge_release.240331-1435
CompositionEditionID : Enterprise
CurrentBuild : 26100
CurrentBuildNumber : 26100
CurrentMajorVersionNumber : 10
CurrentMinorVersionNumber : 0
CurrentType : Multiprocessor Free
CurrentVersion : 6.3
DisplayVersion : 24H2
EditionID : Professional
InstallationType : Client
InstallDate : 1719779588
LCUVer : 10.0.26100.2152
ProductName : Windows 10 Pro This is valid YAML (albeit not intentionally so). What matters for this discussion is that in the first line (and all) the key name is However, if I put it through $YAML = @'
SystemRoot : C:\WINDOWS
BuildBranch : ge_release
BuildLab : 26100.ge_release.240331-1435
BuildLabEx : 26100.1.amd64fre.ge_release.240331-1435
CompositionEditionID : Enterprise
CurrentBuild : 26100
CurrentBuildNumber : 26100
CurrentMajorVersionNumber : 10
CurrentMinorVersionNumber : 0
CurrentType : Multiprocessor Free
CurrentVersion : 6.3
DisplayVersion : 24H2
EditionID : Professional
InstallationType : Client
InstallDate : 1719779588
LCUVer : 10.0.26100.2152
ProductName : Windows 10 Pro
'@
$YAML | ConvertFrom-YAML | Format-List ...the output is as undermentioned: Name : InstallDate
Value : 1719779588
Name : BuildLab
Value : 26100.ge_release.240331-1435
Name : EditionID
Value : Professional
Name : CompositionEditionID
Value : Enterprise
Name : CurrentType
Value : Multiprocessor Free
Name : ProductName
Value : Windows 10 Pro
Name : LCUVer
Value : 10.0.26100.2152
Name : BuildBranch
Value : ge_release
Name : CurrentMajorVersionNumber
Value : 10
Name : CurrentBuildNumber
Value : 26100
Name : CurrentVersion
Value : 6.3
Name : CurrentMinorVersionNumber
Value : 0
Name : DisplayVersion
Value : 24H2
Name : BuildLabEx
Value : 26100.1.amd64fre.ge_release.240331-1435
Name : SystemRoot
Value : C:\WINDOWS
Name : InstallationType
Value : Client
Name : CurrentBuild
Value : 26100 The differences are most visible if I utilize the undermentioned command: $YAML | ConvertFrom-YAML | Format-Table -AutoSize ...where the output is actually more akin to that of Name Value
---- -----
InstallDate 1719779588
BuildLab 26100.ge_release.240331-1435
EditionID Professional
CompositionEditionID Enterprise
CurrentType Multiprocessor Free
ProductName Windows 10 Pro
LCUVer 10.0.26100.2152
BuildBranch ge_release
CurrentMajorVersionNumber 10
CurrentBuildNumber 26100
CurrentVersion 6.3
CurrentMinorVersionNumber 0
DisplayVersion 24H2
BuildLabEx 26100.1.amd64fre.ge_release.240331-1435
SystemRoot C:\WINDOWS
InstallationType Client
CurrentBuild 26100 Questions
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
That is a particularity of Format-List when you give it a Hash Table. It's not encapsulated in anything. A hash table is a key-value pair data structure. As such, Format-List will format a hash table in 2 fields: Name and Value. |
Beta Was this translation helpful? Give feedback.
-
If you are refering to the fact that the resulting deserialized object does not have the keys in the same order as the original yaml, then you can just add the PS /home/gabriel> $YAML | ConvertFrom-YAML -Ordered
Name Value
---- -----
SystemRoot C:\WINDOWS
BuildBranch ge_release
BuildLab 26100.ge_release.240331-1435
BuildLabEx 26100.1.amd64fre.ge_release.240331-1435
CompositionEditionID Enterprise
CurrentBuild 26100
CurrentBuildNumber 26100
CurrentMajorVersionNumber 10
CurrentMinorVersionNumber 0
CurrentType Multiprocessor Free
CurrentVersion 6.3
DisplayVersion 24H2
EditionID Professional
InstallationType Client
InstallDate 1719779588
LCUVer 10.0.26100.2152
ProductName Windows 10 Pro
PS /home/gabriel> $YAML
SystemRoot : C:\WINDOWS
BuildBranch : ge_release
BuildLab : 26100.ge_release.240331-1435
BuildLabEx : 26100.1.amd64fre.ge_release.240331-1435
CompositionEditionID : Enterprise
CurrentBuild : 26100
CurrentBuildNumber : 26100
CurrentMajorVersionNumber : 10
CurrentMinorVersionNumber : 0
CurrentType : Multiprocessor Free
CurrentVersion : 6.3
DisplayVersion : 24H2
EditionID : Professional
InstallationType : Client
InstallDate : 1719779588
LCUVer : 10.0.26100.2152
ProductName : Windows 10 Pro
|
Beta Was this translation helpful? Give feedback.
PsCustomObjects are not hash tables or dictionaries. They are different data structures. Powershell-yaml uses hashtables for mappings. The built in json commandlets use PsCustomObjects.
If you pass a hashtable or dictionary to Format-List it will give you the output you saw.
So the questions are: do you absolutely need PsCustomObjects? Is the issue that Format-List handles hash tables differently than PsCustomObjects? Is there an issue you're encountering when using hashtables instead of PsCustomObjects outside of how Format-list displays the contents of the hashtable?