Luke Puplett
Luke Puplett

Reputation: 45135

PowerShell 3 PSCustomObject displays in stupid mode via a variable

PowerShell 3.0 gave us PSCustomObject as a type which allows proper table formatting, like this:

> [PSCustomObject]@{ Day = "Monday"; Task = "Wash car" }

Day     Task
---     ----
Monday   Wash car

(see http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2012/04/13/Custom-objects-default-display-in-PowerShell-3-0.aspx)

But if I go via a variable, it reverts to stupid mode.

> [PSCustomObject]$row = @{ Day = "Monday"; Task = "Wash car" }
> $row

Name    Value
---     ----
Task    Wash car
Day     Monday

Why? (thanks)

Upvotes: 1

Views: 923

Answers (1)

Luke Puplett
Luke Puplett

Reputation: 45135

Solved it almost immediately.

[PSCustomObject]$row = [PSCustomObject]@{ Day = "Monday"; Task = "Wash car" }

Effectively, I was creating and casting a hashtable to a PSCustomObject. Still not entirely sure why the hashtable keys aren't turned into properties like a .NET 4.0 DLR ExpandoObject.

Much better to write CmdLets in C#.

Upvotes: 1

Related Questions