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 - table type formatted output in powershell

Reply
 
Old 06-05-2009   #1 (permalink)
Umesh Thakur


 
 

table type formatted output in powershell

Hi

I see output of many commands coming in table format, and they look nice.

is there any cmdlet that I can use to format my script's output and print
them in such table format way? in my scenario, my script is reading a
database and i like to display the data in table format.

thanks,
umesh
--
Umesh

"Old programmers never die. They just terminate and stay resident."


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


 
 

RE: table type formatted output in powershell

Check out Format-Table

get-help format-table -full

will give you the details

--
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


"Umesh Thakur" wrote:
Quote:

> Hi
>
> I see output of many commands coming in table format, and they look nice.
>
> is there any cmdlet that I can use to format my script's output and print
> them in such table format way? in my scenario, my script is reading a
> database and i like to display the data in table format.
>
> thanks,
> umesh
> --
> Umesh
>
> "Old programmers never die. They just terminate and stay resident."
>
My System SpecsSystem Spec
Old 06-05-2009   #3 (permalink)
Umesh Thakur


 
 

RE: table type formatted output in powershell

I am sorry, i think I didn't explain it completely.

I am enumerating thru rows of a SQL server table, and I like to print it in
table format in the same way as output of certain cmdlets are shown in
tabular format (eg. get-process).

I tried reading a row, stroring its column values separated by columns in a
string variable and then output it using write-host command and |
format-table piping.. however, its doesn't show in tabular format.


--
Umesh

"Old programmers never die. They just terminate and stay resident."



"RichS [MVP]" wrote:
Quote:

> Check out Format-Table
>
> get-help format-table -full
>
> will give you the details
>
> --
> 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
>
>
> "Umesh Thakur" wrote:
>
Quote:

> > Hi
> >
> > I see output of many commands coming in table format, and they look nice.
> >
> > is there any cmdlet that I can use to format my script's output and print
> > them in such table format way? in my scenario, my script is reading a
> > database and i like to display the data in table format.
> >
> > thanks,
> > umesh
> > --
> > Umesh
> >
> > "Old programmers never die. They just terminate and stay resident."
> >
My System SpecsSystem Spec
Old 06-05-2009   #4 (permalink)
Vadims Podans [MVP]


 
 

Re: table type formatted output in powershell

in your script you can create custom psobject with neccessary properties and
fill them with your data from SQL and stream this object to output using
Format-Table formatting.
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

"Umesh Thakur" <UmeshThakur@xxxxxx> rakstīja ziņojumā
"news:6EE60933-F1AF-49D2-916A-E20BD8BFA979@xxxxxx"...
Quote:

> I am sorry, i think I didn't explain it completely.
>
> I am enumerating thru rows of a SQL server table, and I like to print it
> in
> table format in the same way as output of certain cmdlets are shown in
> tabular format (eg. get-process).
>
> I tried reading a row, stroring its column values separated by columns in
> a
> string variable and then output it using write-host command and |
> format-table piping.. however, its doesn't show in tabular format.
>
>
> --
> Umesh
>
> "Old programmers never die. They just terminate and stay resident."
>
>
>
> "RichS [MVP]" wrote:
>
Quote:

>> Check out Format-Table
>>
>> get-help format-table -full
>>
>> will give you the details
>>
>> --
>> 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
>>
>>
>> "Umesh Thakur" wrote:
>>
Quote:

>> > Hi
>> >
>> > I see output of many commands coming in table format, and they look
>> > nice.
>> >
>> > is there any cmdlet that I can use to format my script's output and
>> > print
>> > them in such table format way? in my scenario, my script is reading a
>> > database and i like to display the data in table format.
>> >
>> > thanks,
>> > umesh
>> > --
>> > Umesh
>> >
>> > "Old programmers never die. They just terminate and stay resident."
>> >
My System SpecsSystem Spec
Old 06-05-2009   #5 (permalink)
Umesh Thakur


 
 

Re: table type formatted output in powershell

thanks for your help Vadims.
I tried to use custom psobject with an example at following site:

http://blog.sapien.com/index.php/200...tputs-a-table/

however, when I print each row, it comes with column headers as well. column
headers shold come only once, and rows should follow. perhaps, I am missing
something or doing something wrong.. you can put me in right direction.

Here is a script that I am writing:

$adOpenStatic = 3
$adLockOptimistic = 3

$objConnection = New-Object -comobject ADODB.Connection
$objRecordset = New-Object -comobject ADODB.Recordset
$dbPath = "d:\test.accdb"
$sql = Read-Host "Enter SQL query to run against the database:"

#This connection string is used for Access 2007 database. for access 2003
DB, use " Microsoft.Jet.OLEDB.4.0"
$objConnection.Open("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =
$dbPath")

$objRecordset.Open($sql, $objConnection,$adOpenStatic,$adLockOptimistic)


Write-Host "Executing following query and displaying result:"
Write-Host $sql

#get all fields of the result
$fieldsCount = $objRecordset.Fields.count
write-host "Total Fields are:" $fieldsCount

$out = new-object psobject #create new psobject for formatted output
for($c = 0;$c -lt $fieldsCount; $c++)
{
$out | add-member noteproperty $objRecordset.Fields.Item($c).name
$objRecordset.Fields.Item($c).name
}

$objRecordset.MoveFirst()
do
{
$out = new-object psobject #create new psobject for formatted output
for($c = 0;$c -lt $fieldsCount; $c++)
{
$out | add-member noteproperty $objRecordset.Fields.Item($c).name
$objRecordset.Fields.Item($c).value
}
write-output $out | ft
$objRecordset.MoveNext()
} until ($objRecordset.EOF -eq $True)


#close recordset and connection object
$objRecordset.Close()
$objConnection.Close()

--
Umesh

"Old programmers never die. They just terminate and stay resident."



"Vadims Podans [MVP]" wrote:
Quote:

> in your script you can create custom psobject with neccessary properties and
> fill them with your data from SQL and stream this object to output using
> Format-Table formatting.
> --
> WBR, Vadims Podans
> MVP: PowerShell
> PowerShell blog - www.sysadmins.lv
>
> "Umesh Thakur" <UmeshThakur@xxxxxx> rakstīja ziņojumā
> "news:6EE60933-F1AF-49D2-916A-E20BD8BFA979@xxxxxx"...
Quote:

> > I am sorry, i think I didn't explain it completely.
> >
> > I am enumerating thru rows of a SQL server table, and I like to print it
> > in
> > table format in the same way as output of certain cmdlets are shown in
> > tabular format (eg. get-process).
> >
> > I tried reading a row, stroring its column values separated by columns in
> > a
> > string variable and then output it using write-host command and |
> > format-table piping.. however, its doesn't show in tabular format.
> >
> >
> > --
> > Umesh
> >
> > "Old programmers never die. They just terminate and stay resident."
> >
> >
> >
> > "RichS [MVP]" wrote:
> >
Quote:

> >> Check out Format-Table
> >>
> >> get-help format-table -full
> >>
> >> will give you the details
> >>
> >> --
> >> 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
> >>
> >>
> >> "Umesh Thakur" wrote:
> >>
> >> > Hi
> >> >
> >> > I see output of many commands coming in table format, and they look
> >> > nice.
> >> >
> >> > is there any cmdlet that I can use to format my script's output and
> >> > print
> >> > them in such table format way? in my scenario, my script is reading a
> >> > database and i like to display the data in table format.
> >> >
> >> > thanks,
> >> > umesh
> >> > --
> >> > Umesh
> >> >
> >> > "Old programmers never die. They just terminate and stay resident."
> >> >
>
My System SpecsSystem Spec
Old 06-05-2009   #6 (permalink)
John Vottero


 
 

Re: table type formatted output in powershell

"Umesh Thakur" <UmeshThakur@xxxxxx> wrote in message
newsCE6B995-91DF-42F9-8A63-8F6D10BE4098@xxxxxx
Quote:

> thanks for your help Vadims.
> I tried to use custom psobject with an example at following site:
>
> http://blog.sapien.com/index.php/200...tputs-a-table/
>
> however, when I print each row, it comes with column headers as well.
> column
> headers shold come only once, and rows should follow. perhaps, I am
> missing
> something or doing something wrong.. you can put me in right direction.
>
Don't do:

write-output $out | ft

Just do:

write-output $out


You should also get rid of the write-host output.

Then, you have a script that outputs objects. When you use the script you
can choose to pipe the output to ft or any other formatter. You can also
sort, select, pick columns etc. If you put the ft into your script, you
only have that choice and your script emits text, not objects.


Quote:

> Here is a script that I am writing:
>
> $adOpenStatic = 3
> $adLockOptimistic = 3
>
> $objConnection = New-Object -comobject ADODB.Connection
> $objRecordset = New-Object -comobject ADODB.Recordset
> $dbPath = "d:\test.accdb"
> $sql = Read-Host "Enter SQL query to run against the database:"
>
> #This connection string is used for Access 2007 database. for access 2003
> DB, use " Microsoft.Jet.OLEDB.4.0"
> $objConnection.Open("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =
> $dbPath")
>
> $objRecordset.Open($sql, $objConnection,$adOpenStatic,$adLockOptimistic)
>
>
> Write-Host "Executing following query and displaying result:"
> Write-Host $sql
>
> #get all fields of the result
> $fieldsCount = $objRecordset.Fields.count
> write-host "Total Fields are:" $fieldsCount
>
> $out = new-object psobject #create new psobject for formatted output
> for($c = 0;$c -lt $fieldsCount; $c++)
> {
> $out | add-member noteproperty $objRecordset.Fields.Item($c).name
> $objRecordset.Fields.Item($c).name
> }
>
> $objRecordset.MoveFirst()
> do
> {
> $out = new-object psobject #create new psobject for formatted output
> for($c = 0;$c -lt $fieldsCount; $c++)
> {
> $out | add-member noteproperty $objRecordset.Fields.Item($c).name
> $objRecordset.Fields.Item($c).value
> }
> write-output $out | ft
> $objRecordset.MoveNext()
> } until ($objRecordset.EOF -eq $True)
>
>
> #close recordset and connection object
> $objRecordset.Close()
> $objConnection.Close()
>
> --
> Umesh
>
> "Old programmers never die. They just terminate and stay resident."
>
>
>
> "Vadims Podans [MVP]" wrote:
>
Quote:

>> in your script you can create custom psobject with neccessary properties
>> and
>> fill them with your data from SQL and stream this object to output using
>> Format-Table formatting.
>> --
>> WBR, Vadims Podans
>> MVP: PowerShell
>> PowerShell blog - www.sysadmins.lv
>>
>> "Umesh Thakur" <UmeshThakur@xxxxxx> rakstīja ziņojumā
>> "news:6EE60933-F1AF-49D2-916A-E20BD8BFA979@xxxxxx"...
Quote:

>> > I am sorry, i think I didn't explain it completely.
>> >
>> > I am enumerating thru rows of a SQL server table, and I like to print
>> > it
>> > in
>> > table format in the same way as output of certain cmdlets are shown in
>> > tabular format (eg. get-process).
>> >
>> > I tried reading a row, stroring its column values separated by columns
>> > in
>> > a
>> > string variable and then output it using write-host command and |
>> > format-table piping.. however, its doesn't show in tabular format.
>> >
>> >
>> > --
>> > Umesh
>> >
>> > "Old programmers never die. They just terminate and stay resident."
>> >
>> >
>> >
>> > "RichS [MVP]" wrote:
>> >
>> >> Check out Format-Table
>> >>
>> >> get-help format-table -full
>> >>
>> >> will give you the details
>> >>
>> >> --
>> >> 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
>> >>
>> >>
>> >> "Umesh Thakur" wrote:
>> >>
>> >> > Hi
>> >> >
>> >> > I see output of many commands coming in table format, and they look
>> >> > nice.
>> >> >
>> >> > is there any cmdlet that I can use to format my script's output and
>> >> > print
>> >> > them in such table format way? in my scenario, my script is reading
>> >> > a
>> >> > database and i like to display the data in table format.
>> >> >
>> >> > thanks,
>> >> > umesh
>> >> > --
>> >> > Umesh
>> >> >
>> >> > "Old programmers never die. They just terminate and stay resident."
>> >> >
>>
My System SpecsSystem Spec
Old 06-05-2009   #7 (permalink)
Vadims Podans [MVP]


 
 

Re: table type formatted output in powershell

> when I print each row, it comes with column headers as well

yes, this occurs when you use Format-Table within loop. You should move this
command outside of loop. Try this:

$(do
{
$out = new-object psobject #create new psobject for formatted output
for($c = 0;$c -lt $fieldsCount; $c++)
{
$out | add-member noteproperty $objRecordset.Fields.Item($c).name
$objRecordset.Fields.Item($c).value
}
$objRecordset.MoveNext()
$out
} until ($objRecordset.EOF -eq $True)) | format-table
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

"Umesh Thakur" <UmeshThakur@xxxxxx> rakstīja ziņojumā
"newsCE6B995-91DF-42F9-8A63-8F6D10BE4098@xxxxxx"...
Quote:

> thanks for your help Vadims.
> I tried to use custom psobject with an example at following site:
>
> http://blog.sapien.com/index.php/200...tputs-a-table/
>
> however, when I print each row, it comes with column headers as well.
> column
> headers shold come only once, and rows should follow. perhaps, I am
> missing
> something or doing something wrong.. you can put me in right direction.
>
> Here is a script that I am writing:
>
> $adOpenStatic = 3
> $adLockOptimistic = 3
>
> $objConnection = New-Object -comobject ADODB.Connection
> $objRecordset = New-Object -comobject ADODB.Recordset
> $dbPath = "d:\test.accdb"
> $sql = Read-Host "Enter SQL query to run against the database:"
>
> #This connection string is used for Access 2007 database. for access 2003
> DB, use " Microsoft.Jet.OLEDB.4.0"
> $objConnection.Open("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =
> $dbPath")
>
> $objRecordset.Open($sql, $objConnection,$adOpenStatic,$adLockOptimistic)
>
>
> Write-Host "Executing following query and displaying result:"
> Write-Host $sql
>
> #get all fields of the result
> $fieldsCount = $objRecordset.Fields.count
> write-host "Total Fields are:" $fieldsCount
>
> $out = new-object psobject #create new psobject for formatted output
> for($c = 0;$c -lt $fieldsCount; $c++)
> {
> $out | add-member noteproperty $objRecordset.Fields.Item($c).name
> $objRecordset.Fields.Item($c).name
> }
>
> $objRecordset.MoveFirst()
> do
> {
> $out = new-object psobject #create new psobject for formatted output
> for($c = 0;$c -lt $fieldsCount; $c++)
> {
> $out | add-member noteproperty $objRecordset.Fields.Item($c).name
> $objRecordset.Fields.Item($c).value
> }
> write-output $out | ft
> $objRecordset.MoveNext()
> } until ($objRecordset.EOF -eq $True)
>
>
> #close recordset and connection object
> $objRecordset.Close()
> $objConnection.Close()
>
> --
> Umesh
>
> "Old programmers never die. They just terminate and stay resident."
>
>
>
> "Vadims Podans [MVP]" wrote:
>
Quote:

>> in your script you can create custom psobject with neccessary properties
>> and
>> fill them with your data from SQL and stream this object to output using
>> Format-Table formatting.
>> --
>> WBR, Vadims Podans
>> MVP: PowerShell
>> PowerShell blog - www.sysadmins.lv
>>
>> "Umesh Thakur" <UmeshThakur@xxxxxx> rakstīja ziņojumā
>> "news:6EE60933-F1AF-49D2-916A-E20BD8BFA979@xxxxxx"...
Quote:

>> > I am sorry, i think I didn't explain it completely.
>> >
>> > I am enumerating thru rows of a SQL server table, and I like to print
>> > it
>> > in
>> > table format in the same way as output of certain cmdlets are shown in
>> > tabular format (eg. get-process).
>> >
>> > I tried reading a row, stroring its column values separated by columns
>> > in
>> > a
>> > string variable and then output it using write-host command and |
>> > format-table piping.. however, its doesn't show in tabular format.
>> >
>> >
>> > --
>> > Umesh
>> >
>> > "Old programmers never die. They just terminate and stay resident."
>> >
>> >
>> >
>> > "RichS [MVP]" wrote:
>> >
>> >> Check out Format-Table
>> >>
>> >> get-help format-table -full
>> >>
>> >> will give you the details
>> >>
>> >> --
>> >> 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
>> >>
>> >>
>> >> "Umesh Thakur" wrote:
>> >>
>> >> > Hi
>> >> >
>> >> > I see output of many commands coming in table format, and they look
>> >> > nice.
>> >> >
>> >> > is there any cmdlet that I can use to format my script's output and
>> >> > print
>> >> > them in such table format way? in my scenario, my script is reading
>> >> > a
>> >> > database and i like to display the data in table format.
>> >> >
>> >> > thanks,
>> >> > umesh
>> >> > --
>> >> > Umesh
>> >> >
>> >> > "Old programmers never die. They just terminate and stay resident."
>> >> >
>>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Writing a formatted directory output to file PowerShell
How to output a powershell into a sql server 2008 table PowerShell
Getting Powershell output into a sql table PowerShell
Re: Writing output to a formatted file PowerShell
Missing data output for custom formatted columns 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