This is a very common pattern:
$os = Get-CimInstance -ClassName Win32_OperatingSystem
$comp = Get-CimInstance -ClassName Win32_ComputerSystem
$props = @{
OS = $os.Caption
InstallDate = $os.InstallDate
LastBoot = $os.LastBootUpTime
Make = $comp.Manufacturer
Model = $comp.Model
}
New-Object -TypeName PSObject -Property $props
Get some data – in this case a couple of WMI classes and create an output object.
Unfortunately, the output looks like this
Make : Microsoft Corporation
Model : Surface Pro 2
LastBoot : 30/12/2016 09:41:52
OS : Microsoft Windows 10 Pro Insider Preview
InstallDate : 08/12/2016 13:20:04
The property order is NOT preserved because you’re working with a hash table. If you absolutely have to preserve the property order use an ordered hash table
$os = Get-CimInstance -ClassName Win32_OperatingSystem
$comp = Get-CimInstance -ClassName Win32_ComputerSystem
$props = [ordered]@{
OS = $os.Caption
InstallDate = $os.InstallDate
LastBoot = $os.LastBootUpTime
Make = $comp.Manufacturer
Model = $comp.Model
}
New-Object -TypeName PSObject -Property $props
All you do is add [ordered] in front of the hashtable definition and your output becomes
OS : Microsoft Windows 10 Pro Insider Preview
InstallDate : 08/12/2016 13:20:04
LastBoot : 30/12/2016 09:41:52
Make : Microsoft Corporation
Model : Surface Pro 2
exactly what you defined