Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - can format-wide display int32 or string as a wide ?

Reply
 
Old 08-21-2009   #1 (permalink)
hayato


 
 

can format-wide display int32 or string as a wide ?

Given
dir | fw -c 8
displays file/directory entries as eight columns in a line. however,
1..8 | fw -c 8
or such
(gc any.txt) | % { if ($_.length -lt 8) {$_}} | fw -c 8
does not work.
Is there a way to make format-wide work ?
Thanks,
-- hayato

My System SpecsSystem Spec
Old 08-21-2009   #2 (permalink)
Robert Robelo


 
 

Re: can format-wide display int32 or string as a wide ?

Hi hayato,

Quote:

> does not work.
Format-Wide, and the rest of 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.

Quote:

> Is there a way to make format-wide work ?
Yes, but some effort is needed

1. Create custom view
2. Add a synthetic member to the String or Int32
3. Use Format-Wide's -Force switch

You can create a custom View for both System.String and System.Int32 Types by saving the following to a PS1XML file:

# let's call it myFormat.ps1xml
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>myView</Name>
<ViewSelectedBy>
<TypeName>System.String</TypeName>
<TypeName>System.Int32</TypeName>
</ViewSelectedBy>
<WideControl>
<WideEntries>
<WideEntry>
<WideItem>
<PropertyName>Me</PropertyName>
</WideItem>
</WideEntry>
</WideEntries>
</WideControl>
</View>
</ViewDefinitions>
</Configuration>
# myFormat.ps1xml

# after you save the file you can update the format data like this:
Update-FormatData -PrependPath myFormat.ps1xml

# we need to add the Me property...
1..99 | Add-Member ScriptProperty Me {$this} -PassThru | fw -c 8 -Force

# you can create a filter that adds the property...
filter Add-Me {
$_ | Add-Member ScriptProperty Me {$this} -PassThru
}

# ... and save some typing
1..99 | Add-Me | fw -c 8 -Force

# you can also use your view on strings
gc any.txt | Add-Me | fw -c 8 -Force

--
Robert
My System SpecsSystem Spec
Old 08-21-2009   #3 (permalink)
Keith Hill [MVP]


 
 

Re: can format-wide display int32 or string as a wide ?

Format-Wide doesn't seem to work on simple, primitive types. You can wrap
them in a PSCustomObject like so:

1..8 | select @{n='num';e={"num $_"}} | fw -c 8

Try modifying your other example to:

gc any.txt | ?{$_.length -lt 8} | select @{n='ShortLine';e={$_}} | fw -c 8

--
Keith

"hayato" <hayato@xxxxxx> wrote in message
news:098C44DF-517D-43D0-BC77-78CD3AC8DEFE@xxxxxx
Quote:

> Given
> dir | fw -c 8
> displays file/directory entries as eight columns in a line. however,
> 1..8 | fw -c 8
> or such
> (gc any.txt) | % { if ($_.length -lt 8) {$_}} | fw -c 8
> does not work.
> Is there a way to make format-wide work ?
> Thanks,
> -- hayato
My System SpecsSystem Spec
Old 08-21-2009   #4 (permalink)
hayato


 
 

Re: can format-wide display int32 or string as a wide ?

Thanks for the reply. Robert, Keith
Both ways work and help me. It is also useful to dump array.
For example,
(gc -enc byte any.bin) | select @{n='num';e={"{0:x2}" -f $_}}| fw -c 16

-- hayato
My System SpecsSystem Spec
Old 08-21-2009   #5 (permalink)
Bob Landau


 
 

Re: can format-wide display int32 or string as a wide ?

This sure ain't intuitive

in .NET lingo you're saying that any value type won't work with Format-Wide?

I don't know what he underpinnings of Format-Wide are but one thinks (at
least me) that it wouldn't have been hard to work with types based off of
System.Value

:{

"Keith Hill [MVP]" wrote:
Quote:

> Format-Wide doesn't seem to work on simple, primitive types. You can wrap
> them in a PSCustomObject like so:
>
> 1..8 | select @{n='num';e={"num $_"}} | fw -c 8
>
> Try modifying your other example to:
>
> gc any.txt | ?{$_.length -lt 8} | select @{n='ShortLine';e={$_}} | fw -c 8
>
> --
> Keith
>
> "hayato" <hayato@xxxxxx> wrote in message
> news:098C44DF-517D-43D0-BC77-78CD3AC8DEFE@xxxxxx
Quote:

> > Given
> > dir | fw -c 8
> > displays file/directory entries as eight columns in a line. however,
> > 1..8 | fw -c 8
> > or such
> > (gc any.txt) | % { if ($_.length -lt 8) {$_}} | fw -c 8
> > does not work.
> > Is there a way to make format-wide work ?
> > Thanks,
> > -- hayato
>
My System SpecsSystem Spec
Old 08-21-2009   #6 (permalink)
Larry__Weiss


 
 

Re: can format-wide display int32 or string as a wide ?

A simplification to
1..99 | Select-Object @{e={"$_"}} | fw -c 8

might better match the formatting of
dir | fw -c 8

Also try
dir | Select-Object @{e={"$_"}} | fw -c 8
It works fine.

I'm now trying to figure out why
$pwd; dir | Select-Object @{e={"$_"}} | fw -c 8
produced this output including the error diagnostic

Path
----
C:\Documents and Settings\Larry\My Documents\WindowsPowerShell
out-lineoutput : Object of type "Microsoft.PowerShell.Commands.Internal.Format.
FormatStartData" is not legal or not in the correct sequence. This is likely ca
used by a user-specified "format-wide" command which is conflicting with the de
fault formatting.
+ CategoryInfo : InvalidData: ( [out-lineoutput], InvalidOperat
ionException
+ FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.P
owerShell.Commands.OutLineOutputCommand

- Larry


Keith Hill [MVP] wrote:
Quote:

> Format-Wide doesn't seem to work on simple, primitive types. You can
> wrap them in a PSCustomObject like so:
> 1..8 | select @{n='num';e={"num $_"}} | fw -c 8
>
> "hayato" <hayato@xxxxxx> wrote in message
> news:098C44DF-517D-43D0-BC77-78CD3AC8DEFE@xxxxxx
Quote:

>> Given
>> dir | fw -c 8
>> displays file/directory entries as eight columns in a line. however,
>> 1..8 | fw -c 8
>> ...
>> does not work.
>> Is there a way to make format-wide work ?
My System SpecsSystem Spec
Old 08-21-2009   #7 (permalink)
Robert Robelo


 
 

Re: can format-wide display int32 or string as a wide ?

You're welcome.

Turns out it can be very simple.
By passing a ScriptBlock with the Current Pipeline Object to Format-Wide's -Property and enabling the Cmdlet's -Force switch is enough.

1..8 | fw {$_} -c 8 -f

gc -enc byte any.bin | fw {"{0:x2}" -f $_} -c 16 -f

Hope all the brouhaha about Format Data, Views and PS1XML files is not useless though

--
Robert
My System SpecsSystem Spec
Old 08-24-2009   #8 (permalink)
Larry__Weiss


 
 

Re: can format-wide display int32 or string as a wide ?

Has anyone taken a look at this?

- Larry

Larry__Weiss wrote:
PS C:> $pwd; dir | Select-Object @{e={"$_"}} | fw -c 8
produces an error...

PS C:> $pwd;
PS C:> dir | Select-Object @{e={"$_"}} | fw -c 8
does not produce an error.

and neither does
PS C:> $home; dir | Select-Object @{e={"$_"}} | fw -c 8

What's going on?
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: Xerox Wide Format Printer 7142 Vista print fax & scan
Cannot print wide format HP CP1700 Vista print fax & scan
Help Desktop has gone too wide Vista General
Is there a way to get Format-Wide to sort by column? PowerShell
RuntimeType format uses excessively wide name column PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46