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

Format Output (like printf)

409 views
Skip to first unread message

Tom

unread,
May 20, 2016, 1:37:09 PM5/20/16
to
Greetings,

Got the following code off the web:

function FormatString {
param([string]$partnum, [string]$descrip, [string]$refdes)
$obj = New-Object PSObject
$obj | Add-Member NoteProperty PartNumber($pn)
$obj | Add-Member NoteProperty Description($des)
$obj | Add-Member NoteProperty RefDes($r)
Write-Output $obj
}

FormatString $pn $des $r

This works nicely, however the output is a bit off from Column 1 to Column2:

PartNumber Description RefDes
---------- ----------- ------
011030-01-LF [I_ROHS6] ICKT SINGLE 2-INPUT POSITIVE AND GATE 1.6-5.5V 24MA 5... U10
011030-01-LF [I_ROHS6] ICKT SINGLE 2-INPUT POSITIVE AND GATE 1.6-5.5V 24MA 5... U14

I'd like Column1 to be about 10 characters wide and Column2 to be 120. Sort of what printf would do:

printf("%10s, %120s, %4s\n", $pn, $des, $r)

I've tried playing around with format:

@("$pn", "$des", "$r") | FormatString | Format-Table -Autosize

without any luck as to what I'm looking for. What am I missing or doing wrong?

thanks
Tom

Jürgen Exner

unread,
May 20, 2016, 9:39:01 PM5/20/16
to
On Fri, 20 May 2016 10:37:07 -0700 (PDT), Tom <lyn...@gmail.com> wrote
in microsoft.public.windows.powershell:

>FormatString $pn $des $r
>
>This works nicely, however the output is a bit off from Column 1 to Column2:
>
>PartNumber Description RefDes
>---------- ----------- ------
>011030-01-LF [I_ROHS6] ICKT SINGLE 2-INPUT POSITIVE AND GATE 1.6-5.5V 24MA 5... U10
>011030-01-LF [I_ROHS6] ICKT SINGLE 2-INPUT POSITIVE AND GATE 1.6-5.5V 24MA 5... U14
>
>I'd like Column1 to be about 10 characters wide and Column2 to be 120. Sort of what printf would do:
>
>printf("%10s, %120s, %4s\n", $pn, $des, $r)


Please take a look at
http://stackoverflow.com/questions/9904352/how-to-create-printf-effect-in-powershell
I think that answers yoru questions nicely.

jue
0 new messages