Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to display array of strings via Format-Wide

0 views
Skip to first unread message

stej

unread,
Jan 2, 2010, 6:17:03 PM1/2/10
to
Has anybody tried to display collection of strings using Format-Wide?
It works, but everytime only one column is used.

Example:
[1]:> '1', '2', '3', '4', '5' | format-wide -autosize
1
2
3
4
5

I have no idea how to persuade Posh to use more columns or accept -
autosize switch.

The only way that works is this one:
'1','22','333','4444','55555' | % { new-object psobject -prop @{This=
$_}} | Format-Wide -Property This -autosize
i.e. creating new psobject with a noteproperty

Rob Campbell

unread,
Jan 4, 2010, 8:51:01 AM1/4/10
to
[string]@('1','2','3','4','5') | format-wide -autosize

"stej" wrote:

> .
>

stej

unread,
Jan 4, 2010, 4:55:33 PM1/4/10
to
Hm, what if I'd like to have 3 columns?

On Jan 4, 2:51 pm, Rob Campbell

Robert Robelo

unread,
Jan 4, 2010, 5:16:35 PM1/4/10
to
Pass the object in a scriptblock and use the -Force switch:

'1', '2', '3', '4', '5' | Format-Wide {$_} -AutoSize -Force
'1', '22', '333', '4444', '55555' | Format-Wide {$_} -AutoSize -Force

--
Robert

Tome Tanasovski

unread,
Jan 4, 2010, 10:27:01 PM1/4/10
to
Can you explain this? What is the {$_} doing differently, and why does it
require -force?

Robert Robelo

unread,
Jan 5, 2010, 12:25:44 AM1/5/10
to
I can try... :)

Passing the current object in the pipeline $_ in a ScriptBlock ensures that the Cmdlet displays the Object and not one of the Object's properties to display.
You could also pass the $_ in a calculated property as a Hashtable instead.

The Format-* Cmdlets rely on a predetermined structure (View) provided by the system or extended by the user.
These Views target specific Types and, most of the time, one or more of their properties.
When a Type does not have a View, the system uses the default view.
This is why Format-Wide returns each object in its own row, instead of columns.
To coerce Fomat-Wide to _not_ implement the default view, use the -Force switch.

# pass the current object in the pipeline $_ in a ScriptBlock
'1', '2', '3', '4', '5' | Format-Wide {$_} -AutoSize -Force

# passing the current object in the pipeline $_ in a calculated property
'1', '2', '3', '4', '5' | fw @{Expression = {$_}} -AutoSize -Force


# here the Object's type is String, and String's Length is displayed
'1', '2', '3', '4', '5' | Format-Wide -AutoSize -Force

--
Robert

stej

unread,
Jan 5, 2010, 2:16:53 AM1/5/10
to
@robert, thank you very much. You explanation leads me to this: "if
something looks strange, try to use -force param" :) Even the
documentation (help Format-Wide -Parameter force) says something like
that: " Overrides restrictions that prevent the command from
succeeding..."

On Jan 5, 6:25 am, "Robert Robelo" <Ki...@HighPlainsDrifter.com>
wrote:

Tome Tanasovski

unread,
Jan 5, 2010, 10:09:02 AM1/5/10
to
That made my morning! That little piece of documentation is officially my
favorite help blurb ever! Actually, I never had a favorite help blurb until
now.

Thanks for the explanation


"stej" wrote:

> .
>

Robert Robelo

unread,
Jan 5, 2010, 4:43:21 PM1/5/10
to
Glad to help guys.

--
Robert
0 new messages