chris
chris

Reputation: 33

How to create PowerShell datastructure that works well with format-table

When I do "$states2 | ft" I get this:

Name                           Value
----                           -----
NY                             State
AZ                             State

When I would rather get this:

Name    Acres   Population   Founded  FullName
----    -----   ----------   -------  --------
NY        50           100      1645  New York
AK       100           512      1745  Alaska

What am I doing wrong?

Here is my data structure in the form of a class:

class State {
    [Int64]$Acres
    [Int64]$Population
    [Int16]$Founded
    [string]$FullName
    }

$States2 = @{}
$States2.AK = [State] @{Population = 512; Founded = 1745; Acres = 100; FullName = "Alaska"}

$States2.NY = [State]::new()
$States2.NY.Acres       = 50
$States2.NY.Population = 100
$States2.NY.Founded    = 1652
$States2.NY.FullName   = "New York"

Thanks in advance

Upvotes: 2

Views: 93

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

You'll need to make the abbreviated name part of the class definition:

class State {
    [string]Name
    [Int64]$Acres
    [Int64]$Population
    [Int16]$Founded
    [string]$FullName
}

$States2 = @{}
$States2.AK = [State] @{Name = "AK"; Population = 512; Founded = 1745; Acres = 100; FullName = "Alaska"}

$States2.NY = [State]::new()
$States2.NY.Name       = "NY" 
$States2.NY.Acres      = 50
$States2.NY.Population = 100
$States2.NY.Founded    = 1652
$States2.NY.FullName   = "New York"

If you only want the show the values in the $States2 hashtable, do:

$States2.Values |Format-Table

Upvotes: 2

Related Questions