Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does powershell rearrange my output? [duplicate]

Tags:

powershell

I build the following CustomObject in my function

    New-Object -Type PSCustomObject -Property @{
        Computername      = $_
        PowerShellVersion = $result[0]
        dotNetVersion     = $result[1]
        sqlnacli          = $result[2]
        redistributable   = $result[3]
    }

But the output is like this:

PowerShellVersion Computername redistributable sqlnacli dotNetVersion
----------------- ------------ --------------- -------- -------------
3+ OK             SERVERNAME     NOT OK          NOT OK   NOT OK    

why does PowerShell rearrange the order of my object and how can I force it to take the order I want it to?

like image 539
SimonS Avatar asked Feb 17 '17 14:02

SimonS


People also ask

How to remove duplicates from PowerShell output?

Use Get-Unique to Remove Duplicates From PowerShell Array It compares each item in a sorted list to the next item, removes duplicates, and returns only one member of each item. This cmdlet only works if the list is sorted. You can use the Sort-Object cmdlet to sort objects by property values.

How do I redirect output in PowerShell?

There are two PowerShell operators you can use to redirect output: > and >> . The > operator is equivalent to Out-File while >> is equivalent to Out-File -Append . The redirection operators have other uses like redirecting error or verbose output streams.


2 Answers

Hashtables are not ordered by definition. If you have PowerShell v3.0 or newer, you can use the [Ordered] attribute for the hashtable:

New-Object PSCustomObject -Property ([Ordered] @{
  Computername      = $_
  PowerShellVersion = $result[0]
  dotNetVersion     = $result[1]
  sqlnacli          = $result[2]
  redistributable   = $result[3]
})

In PowerShell v3+ you can also just use the [PSCustomObject] type accelerator:

[PSCustomObject] @{
  Computername      = $_
  PowerShellVersion = $result[0]
  dotNetVersion     = $result[1]
  sqlnacli          = $result[2]
  redistributable   = $result[3]
}

If you need PowerShell version 2 compatibility, then you can use

New-Object PSObject -Property @{
  Computername      = $_
  PowerShellVersion = $result[0]
  dotNetVersion     = $result[1]
  sqlnacli          = $result[2]
  redistributable   = $result[3]
} | Select-Object Computername,PowerShellVersion,dotNetVersion,sqlnacli,redistributable

The drawback with this method is if you add properties, you have to remember to add them to Select-Object.

An alternative PowerShell v2 or older is to output something (e.g., an empty string) and then use Select-Object to create exactly the custom properties you want:

$obj = $_
"" | Select-Object `
  @{Name = "Computername";      Expression = {$obj}},
  @{Name = "PowerShellVersion"; Expression = {$result[0]}},
  @{Name = "dotNetVersion";     Expression = {$result[1]}},
  @{Name = "sqlnacli";          Expression = {$result[2]}},
  @{Name = "redistributable";   Expression = {$result[3]}}

The only (minor) drawback here is that $_ in Select-Object refers to the the immediately preceding object from which properties are being selected (hence the $obj = $_ as the first line, to preserve the value).

like image 138
Bill_Stewart Avatar answered Oct 12 '22 15:10

Bill_Stewart


As @wOxxOm and @Bill_Stewart have mentioned, you need an ordered hashtable to dictate the order and by default, standard hashtables aren't. You can influence the order of a normal hashtable with a select statement, like this.

New-Object -Type PSCustomObject -Property @{
    Computername      = $_
    PowerShellVersion = $result[0]
    dotNetVersion     = $result[1]
    sqlnacli          = $result[2]
    redistributable   = $result[3]
} | Select-Object Computername,PowerShellVersion,dotNetVersion,sqlnacli,redistributable
like image 27
TechSpud Avatar answered Oct 12 '22 14:10

TechSpud