PDQ.com mobilePDQ.com desktop
Support

PowerShell Tip: Formatting Output

Adam RuthAdam Ruth
·

In PowerShell everything is a .NET object, each with a variety of different properties. You’ll notice that most of the time the output you see from basic commands only gives you a small subset of what’s available. This is because PowerShell has built-in rules for how and what to display for most of the standard objects. Usually this is done so that you normally only see what’s most important and aren’t overwhelmed with too much data. For example, the dir command returns a set of FileSystemInfo objects but only shows you a few of the 20+ properties that are available.

You can override the default output for any object and PowerShell provides a set of cmdlets for just this purpose: Format-Wide, Format-Table, Format-List, and Format-Custom. I’ll cover Format-Custom in another post, so for now here’s a brief description of the first 3.

Format-Wide

This cmdlet simple takes one property of each object and writes them out across the window. This is similar to the /w option when using dir in cmd.exe.

Notice that to use the cmdlet you need to pipe the output of the dir command to Format-Wide. Unlike the pipe command in cmd.exe, you aren’t just sending the text from the first command but actual .NET objects which allows the second command to have full control when dealing with them.

With Format-Wide you can select the property to show with the -Property parameter. You can also adjust the width of the columns with -Column.

Format-Table

This cmdlet formats objects just like the default dir command, but you have control over what properties show up and how they are formatted. For example:

In order to adjust the widths of the columns independently you need to create a View and pass it to the cmdlet, but that’s a topic for its own posting. You can even group the output by unique properties of the files.

Format-List

This cmdlet formats the output as a list of properties. It’s similar to the table output, but rotated 90 degrees.

Just like Format-Table you can decide which properties to show. You can use wildcards to select multiple properties (this also works with Format-Table.)

If you just use * for the property list you’ll see all of the properties of the object. This is handy while exploring objects to see what information you can work with.

Hopefully this quick overview will get you exploring the different ways to output data in PowerShell. In future posts I’ll discuss more advanced topics such as Format-Custom, Views, and Calculated Properties.

Did you know that PDQ Deploy has a PowerShell step you can use to deploy your scripts?


This blog post is part of a series on PowerShell:

Ready to put the PowerShell scanner through its paces?

Take our 14-day Free Trial. Feel the power of the PowerShell scanner.
Start a Trial

Don't miss the next post!

Using PowerShell to Install Printers

When installing printers, we will need to do the four things; Add Driver to the Store, Install the Driver, Create Printer Port, and Install the Printer