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 - Output all content of text file in different columns

Reply
 
Old 06-01-2008   #1 (permalink)
Jason1008


 
 

Output all content of text file in different columns

Below is a simple script that i've done. I'm willing to output all the
content of the file "test.txt" when i run the command "$mycol | format-table
-auto". Unfortunately the only result that i get is the last input.

$a = read-host "Num"
$b = read-host "Num"
$c = read-host "Num"

$d = "$a : $b : $c"

write-output "$d" | out-file "f:\test.txt" -append


$mycol = @()

$colItems = get-content f:\test.txt

foreach ($d in $colItems) {
$myobj = "" | Select-Object A,B,C
$myobj.A = $a
$myobj.B = $b
$myobj.C = $c
$myCol = $myobj

}

$mycol | format-table -auto

My System SpecsSystem Spec
Old 06-01-2008   #2 (permalink)
RichS [MVP]


 
 

RE: Output all content of text file in different columns

The main reason you are only seeing the last input is that you are setting
your colums to equal what you just input

$myobj.A = $a
$myobj.B = $b
$myobj.C = $c

You used $a, $b and $c as your input variables for the three read-host lines

An easier way to display the contents is like this. Change your output
file to a csv file and give it a header row of A,B,C. Change your script to

$a = read-host "Num"
$b = read-host "Num"
$c = read-host "Num"

$d = "$a,$b,$c"

out-file -InputObject $d -FilePath "c:\test\test2.csv" -append

import-csv "c:\test\test2.csv" | format-table -auto


Your file will look like this

A,B,C
5,6,7
1,2,3

etc

--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Jason1008" wrote:
Quote:

> Below is a simple script that i've done. I'm willing to output all the
> content of the file "test.txt" when i run the command "$mycol | format-table
> -auto". Unfortunately the only result that i get is the last input.
>
> $a = read-host "Num"
> $b = read-host "Num"
> $c = read-host "Num"
>
> $d = "$a : $b : $c"
>
> write-output "$d" | out-file "f:\test.txt" -append
>
>
> $mycol = @()
>
> $colItems = get-content f:\test.txt
>
> foreach ($d in $colItems) {
> $myobj = "" | Select-Object A,B,C
> $myobj.A = $a
> $myobj.B = $b
> $myobj.C = $c
> $myCol = $myobj
>
> }
>
> $mycol | format-table -auto
My System SpecsSystem Spec
Old 06-01-2008   #3 (permalink)
Kiron


 
 

Re: Output all content of text file in different columns

Split the line, trim each split value, and set the object's properties through multiple assignment. Use the += operator to append the new object to the collection:

$mycol = @()
$colItems = get-content f:\test.txt
foreach ($d in $colItems) {
$myobj = "" | Select-Object A,B,C
$myobj.A, $myobj.B, $myobj.C = $d.split(':') | % {$_.trim()}
$myCol += $myobj
}
$mycol | format-table -auto

--
Kiron
My System SpecsSystem Spec
Old 06-04-2008   #4 (permalink)
Jason1008


 
 

Re: Output all content of text file in different columns

Thank a lot. It work perfectly.



"Kiron" wrote:
Quote:

> Split the line, trim each split value, and set the object's properties
> through multiple assignment. Use the += operator to append the new object
> to the collection:
>
> $mycol = @()
> $colItems = get-content f:\test.txt
> foreach ($d in $colItems) {
> $myobj = "" | Select-Object A,B,C
> $myobj.A, $myobj.B, $myobj.C = $d.split(':') | % {$_.trim()}
> $myCol += $myobj
> }
> $mycol | format-table -auto
>
> --
> Kiron
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
getting text file content from remote computer PowerShell
Using Add-Content to add text to a certain location of a file. PowerShell
output .ps1 script to text file PowerShell
Reccording DOS output to a text file Vista security


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