Format Table

rated by 0 users
This post has 2 Replies | 2 Followers

Not Ranked
Posts 2
Aravind Posted: 03-09-2012 6:49 AM

Hi

I am new to Power shell. As am trying to get output result in a table format.  But still am getting output in a single column. 

I am getting output as below:

Alerter
Stopped
ALG
Running
AppMgmt
Stopped
aspnet_state
Stopped

But I need as below format:

 Name                            State                   
 -------- ----                       -----                 
  Alerter                         Stopped                  
    ALG                           Running                    
  AppMgmt                     Stopped                    
 aspnet_state                Stopped

 

 

Below is the command am going try to get "Win32_Service" Names and its State:

----------------------------------------------------------------------------------

cls
$column1 = @{expression="Name"; width=20;label="Name"; alignment="left"}
$column2 = @{expression="State"; width=10;label="State"; alignment="right"}
$c = Get-WmiObject Win32_Service | ForEach-Object {$_.Name,$_.State} 
$c | Format-Table $column1, $column2 > "G:\study\power shell\procprop1.txt"

----------------------------------------------------------------------------------------------------------

And also I tried below Command:

------------------------------------------------------------------------------------------------------------

cls
$c = Get-WmiObject Win32_Service | ForEach-Object {$_.Name,$_.State} 
$c | Format-Table Name,State > "G:\study\power shell\wmiservice.txt"

 

Top 25 Contributor
Posts 335
Microsoft MVP
Top Contributor

my first rule of formatting: keep it simple.

 

1. You can do this easily by:

Get-Service | format-list name,state -autosize

 

2. Using hash tables - you specified expression wrongly - it should look like this:

$column1 = @{expression={$_.Name}; width=20;label="Name"; alignment="left"}

$column2 = @{expression={$_.Status}; width=10;label="State"; alignment="right"}

3. Putting it all together, you can simplify:

$column1 = @{expression="$_.Name"; width=20;label="Name"; alignment="left"}

 

Top 25 Contributor
Posts 335
Microsoft MVP
Top Contributor

THat last line is wrong and I can't edit it.

 

Putting it all together, you can do like this:

Get-WmiObject Win32_Service | Format-Table $column1, $column2

You don't need all the other verbiage in your original post.

Page 1 of 1 (3 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.