Format-Table problem

Hi Forum,

when I write:

$a = gwmi -class win32_service -filter "Caption like '%sql%'" | ft Name, Caption, Status, StartMode, Started, StartName, State -a
foreach($b in $a)
{
    $b.Caption
}   
$a.gettype() 

..then foreach gives nothing ($b.caption) back.
$a.gettype() gives back:
IsPublic IsSerial Name    BaseType  -------- -------- ----                                     --------                                                                                                                                         
True     True     Object[]                                 System.Array

when I change the code to:

$a = gwmi -class win32_service -filter "Caption like '%sql%'" 
foreach($b in $a)
{
    $b.Caption
}   
$a.gettype() 
all works fine and I get the captions.
In both cases $a.gettype() shows the same.
Why is Format-Table messing up the value of the variable $a? Where can I find (get-help?) the reason for that behavior?
Thanks for your help Purclot
  • Edited by Purclot Thursday, January 29, 2015 1:43 PM
January 29th, 2015 4:39pm

Hi Purclot,

you can find the help for that in the Format-Table documentation (Get-Help Format-Table -Full; Check out the OUTPUTS section). Format Table takes objects and converts it into information the console knows how to display. Datawise it's a dead end.

Since in both variations you have multiple items, they are a stored in an Array (whose GetType() you call).

Cheers,

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2015 4:51pm

See Fred's response for reasons why, but generally you should use Select-Object instead of Format-* (unless you're meaning to display output in the console).
January 29th, 2015 5:12pm

Fred, thanks, as always: read the friendly manual!
Purclot
Free Windows Admin Tool Kit Click here and download it now
January 29th, 2015 5:26pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics