![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||