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.

Advertisements
This entry was posted in Powershell Basics. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s