header image

Formating multiple outputs

Posted by: | December 11, 2015 Comments Off on Formating multiple outputs |

Using Get-WmiObject and Get-Service you can do this:

PS> Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’BITS’” | select StartMode

StartMode
———
Auto

PS> Get-Service -Name BITS | select Status

Status
——
Running

 

If you try running the two commands in a script, in ISE or even like this:

PS> Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’BITS’” | select StartMode; Get-Service -Name BITS | select Status

StartMode
———
Auto

 

You only get the first result.  If you reverse the order of the commands:

PS> Get-Service -Name BITS | select Status; Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’BITS’” | select StartMode

Status
——
Running

 

You still get the first result.

 

When you run a pipeline the results are automatically piped to Out-Default. The formatting system then decides to use a table format because you have less than 5 properties.

 

When the commands are run individually each calls Out-Default individuallly. When the 2 commands are run in a script (or other option that causes them to execuate together) the formatting takes its direction from the first object it receives and because the second object doresn’t have matching properties nothing is displayed.

 

The answer is to manually force each command to pipe to out-default

PS> Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’BITS’” | select StartMode | Out-Default; Get-Service -N
ame BITS | select Status | Out-Default

StartMode
———
Auto

 

Status
——
Running

 

OR

PS> Get-Service -Name BITS | select Status | Out-Default; Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’B
ITS’” | select StartMode | Out-Default

Status
——
Running

 

StartMode
———
Auto

 

This is a common issue that users trip over as they learn PowerShell.

under: PowerShell Basics

Comments are closed.

Categories