Format Table

rated by 0 users
Not Answered This post has 0 verified answers | 2 Replies | 2 Followers

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


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:


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:


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


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


All Replies

Top 25 Contributor
350 Posts
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
350 Posts
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 All rights reserved.