This thing will take a single column of text, split it up into columns and put it on screen - But it could be adapted easily to some other output.
A couple of things to note:
- '$pad' is there for readability - so the columns have some padding.
- '-HideTableHeaders'
- The 'Function' items inside this, especially the 'ListToArray' comes in handy for me all the time. I dropped it in this way, just to share it in a way that that stood out.
- This could be done in fewer lines, and I do like brevity, but I wanted to leave it more 'spelled' out, for illustrative purposes.
Using the 50 states to show it working.
Function '#of columns' "Data source"
Column2Columns 3 $Data Column2Columns 5 $Data Column2Columns 10 $Data
|
Examples below the code:
$Data =
("Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware Florida Georgia Hawaii Idaho Illinois Indiana Iowa Kansas Kentucky Louisiana Maine Maryland Massachusetts Michigan Minnesota Mississippi Missouri Montana Nebraska Nevada New Hampshire New Jersey New Mexico New York North Carolina North Dakota Ohio Oklahoma Oregon Pennsylvania Rhode Island South Carolina South Dakota Tennessee Texas Utah Vermont Virginia Washington West Virginia Wisconsin Wyoming")
Function Column2Columns { ################################ <# .DESCRIPTION Takes a single column of data (as a string), splits it into a
specified number of columns, as its output.
.EXAMPLE #
Three columns Column2Columns 3 $Data
.EXAMPLE #
Two columns Column2Columns 2 $Blah
.EXAMPLE #
Four columns Column2Columns
4 $LongList #> Param ( [Parameter(Mandatory=$true, Position=0)] [int]
$Columns, [Parameter(Mandatory=$true, Position=1)] [string] $Data )
Function ListToArray { ($Args).Split("`n|`r",[System.StringSplitOptions]::RemoveEmptyEntries) } Function AddMembr {$Stack | Add-Member -membertype noteproperty -name "Item$Counter_Data" -Value "$_$pad"} $pad = "
" $Counter_Data = 0 $Columns_Array =
@() $Stack = new-object psobject
ListToArray $Data |
% { $Counter_Data++ If
($Counter_Data -lt
($Columns)) { AddMembr
} If
($Counter_Data -ge
($Columns)) { AddMembr $Counter_Data = 0 $Columns_Array += $Stack $Stack = new-object psobject } }
$Columns_Array += $Stack $Columns_Array = $Columns_Array | ? { !([string]::IsNullOrEmpty($_)) } $Columns_Array | ft -HideTableHeaders } # END Function
|
Examples:
No comments:
Post a Comment