Say what’s up to PowerShell 5 (Part 19/27)

In this quick post I want to you show you a really easy way to format your output.  You’ve already seen how to filter columns, sort output and remove unwanted information with where-object.  Now I want to show you how to spruce up the final result a bit.

When it comes to formatting you really only need three cmdlets:

  • Format-List
  • Format-Wide
  • Format-Table

First up: Format-List

Format-List is perfect for when you’re doing troubleshooting since it shows you all the properties at once.

So you can pipe Get-Service out to Format-List and select all properties so you see all the values.

Here’s how you could return all the properties for the first 3 services:

Get-Service | Select-Object -First 2 | Format-List *

or

gsv | select -first 2 | fl*

format-list

Oh look at all those properties! hehe.

This is a great way to troubleshoot because you’re getting every single feature of the object that’s being sent to Format-List 

Go wide with Format-Wide

Format-Wide is perfect for when you’re dealing with a single property.

Let’s say you’re looking at some output but it only returns a single column.  In other words, you have a collection of objects that only shows you a single property.

A single propety

You’re going to have a lot of scrolling to do here.  And it’s why, it’s a perfect candidate for Format-Wide.

If you use the Format-Wide -Name parameter you can filter on just the name column and make it fill the screen with columns (instead of just scrolling down the screen)

get-service | format-wide -property name

Also, since the -property parameter is positional you could just type it like this:

get-service | format-wide name

Format-Wide name

or even better like this with the fw Format-Wide alias:

gsv | fw name

And you can even change the number of columns like so:

gsv | fw name -col 5

Format-Wide columns

Only four columns are present in the above screenshot but if I scroll to the right the fifth one is visible.

Format-Table is able

Everyone loves Format-Table and it’s one you’ll begin to love too.  FT is the alias.

gsv | ft

PowerShell Format-Table

At first, Format-Table looks like Select-Object

gsv | select status,name,displayname

Select-Object

But it’s not really the same thing.  Mainly because when you use Format-Table you terminate the Pipeline.  You can’t continue to pipe the results to another cmdlet.  But Select-Object doesn’t end the pipeline so you can continue piping and tweaking the results.

Playing with Format-Table

Alright, so let’s say you’re digging through the event logs and you realize you just need the last 3 more recent events.  You want to see the type of EventLog, where it came from and the timestamp.

You could type:

get-eventlog system | select-object -first 3 | format-table -property EntryType, Source, TimeGenerated

format-table example

That’s cool.  I mean you can see when the event was generated, but wouldn’t it be cool if you could also see how long ago it was generated?  That way instead of doing the math in your head you could instantly see exactly how long ago it was when an event fired.

This means we need the date.  If we could subtract the current time (whatever that is) from the time when the event was generated we would have our new awesome property.

Type Get-Date in the shell.

Get-Date

Cool, let’s create a hash table column and generate our computed content.

@{n='How long ago';e={$_.TimeGenerated}}

Now let’s combine that with our original cmdlet:

get-eventlog system | select-object -first 3 | format-table -property EntryType, Source, TimeGenerated, @{n='How long ago';e={((Get-Date)-$_.TimeGenerated)}}

Get-Date minus Time-Generated

So far you can do all that with Select-Object

But here’s how Format-Table differs: we have more formatting controls.

For example, I can add:

align='right';

and

formatstring='dd\.hh\:mm\:ss'

So the time is right aligned and in a days, hours, minutes, seconds format.

Check it out:

get-eventlog system | select-object -first 3 | format-table -property EntryType, Source, TimeGenerated, @{n='How long ago';e={((Get-Date) $_.TimeGenerated)};align='right';formatstring='dd\.hh\:mm\:ss'}

FormatString

Beautiful isn’t it?

Remember, you wouldn’t type this out everytime.  You would bundle this into a script so that you’ve done all the work up front and then all someone needs to do is run your script (we’ll talk about Scripts in a future post)

 

About

Connect with Vonnie on Twitter

Posted in Windows, Windows 10 Tagged with: