Ask the Expert

How can I make my PowerShell output look better?

How do I better format the PowerShell output?

    Requires Free Membership to View

This is a really common question, and it doesn't always have a great answer.

First of all, consider this command:

Get-Process | Select-Object –Property Name,ID,VM,PM,CPU | Format-Table

Unless you specify a format command, as I've done in this case, PowerShell's native formatting system handles the PowerShell output for any command. You will get the same capabilities, and they're fairly primitive. If you're trying to produce awesome-looking reports for the boss, lower your expectations. PowerShell isn't designed for that.

HTML lets you do a bit more:

Get-Process |
Select-Object –Property Name,ID,VM,PM,CPU |
ConvertTo-HTML –Title " Report" –CssUri http://intranet/admins/style.css |
Out-File \\intranet\wwwdocs\admins\procreport.html

This command converts the output from PowerShell to an HTML table and gives it a nice title and links to a style sheet that can provide visual formatting. Keep in mind: This approach can only take you so far. The HTML produced is pretty simplistic and, unless you start injecting JavaScript code, you can only do so much.

More robust approaches are available, and there are two free ebooks specifically on the topic. Both are listed here. One approach lets you produce HTML-based reports that can contain multiple sections, dynamic tables and much richer visual formatting. The result is still an HTML file, so it can go on an intranet Web server or be attached to an email.

The second approach inserts data into a SQL Server table (even a free Express edition) and uses SQL Server Reporting Services (also free) to produce rich heavily formatted reports. The second approach works well for trans-reports and other historical data, and it requires little programming to be successful.

About the author
Don Jones is a well-known and respected PowerShell expert and educator. He's co-author of three books on PowerShell (see PowerShellBooks.com for a list). You can find his content online, including his PowerShell Q&A forums, by visiting DonJones.com.

This was first published in April 2013

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: