1

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)

1 Answer 1

1

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#.

Sign up to request clarification or add additional context in comments.

1 Comment

Just want to note that in PowerShell 2, this would still result in type Hashtable, not PSCustomObject. Had this same exact issue and couldn't figure it out till I saw this question and realized that my issue is the PowerShell version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.