-1

The below script is running in a for-each loop and the output heading is not coming in the right order. I want the output order as I mentioned in the hash table. I want the CSV table column heading in right order, not the data inside the table.

Like 1.Machine_Name, 2.Ping_Status, 3.OS_Name

How can I solve this?

$Result = @{
            MACHINE_NAME     = "$_"
            PING_STATUS      = "MACHINE ONLINE"
            OS_NAME = "$OSNAME"
        }

$Details += New-Object PSObject -Property $Result
$Details | export-csv -Path $pathofcsv -NoTypeInformation
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
VivekRR
  • 15
  • 10

2 Answers2

7

Use an ordered hash table:

$Result = [ordered]@{             
            MACHINE_NAME     = "$_" 
            PING_STATUS      = "MACHINE ONLINE"
            OS_NAME = "$OSNAME"
        }

$Details += New-Object PSObject -Property $Result
$Details | export-csv -Path $pathofcsv -NoTypeInformation
mjolinor
  • 66,130
  • 7
  • 114
  • 135
  • Thank you mjolinor... I already used [ordered] key, but no luck. Actually Machine_Name, Ping_status and OS_name is the heading of a csv. I want the heading column in right order, not the data inside file. – VivekRR Mar 16 '15 at 15:24
  • The column headings should be in the same order that the hash table enumerates. If it's an ordered hash table, it should enumerate in the same order that the entries were defined. – mjolinor Mar 16 '15 at 15:59
2

To force the order, use Select-Object:

$Details | Select-Object MACHINE_NAME, PING_STATUS, OS_NAME | export-csv -Path $pathofcsv -NoTypeInformation
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Martin
  • 11,764
  • 1
  • 61
  • 74