#PowershellBasics: Expanding the column width to avoid truncation

3

July 7, 2022 by Kenneth Fisher

I was recently asked to get a list of all of the services running on one of the boxes we have SQL Server Instances running on. Which was simple enough using Get-Service. There are even some handy filters you can use to get patterns. For example all services with Microsoft in the description. In this case I’m going to use Where-Object to just get the running services. I should point out here that the script I’m using came directly from the examples in the online docs and that I’m running this on my laptop not an actual server.

Get-Service | Where-Object {$_.Status -eq "Running"}

Notice the ellipsis (the three dots). That’s showing us that the name was too long and ended up being truncated. Given that I’ve been doing this for a little while now I’m almost completely certain that if I send this as it is the users are going to want to know full names. And with my luck I’ll end up having to give them each truncated string individually. On the theory that if I have time to do it twice I probably have time to do it right the first time, let’s figure out how to expand the columns. Fortunately, as with most things Powershell, there’s a cmdlet for that. Format-Table, which is specifically designed for, you guessed it, formatting the output table for other cmdlets. Using the AutoSize parameter tells it I want the full column width based on the size of the data.

Get-Service | Where-Object {$_.Status -eq "Running"} | Format-Table -AutoSize

Yay! No ellipsis!

On top of that, Format-Table has, among other things, a cool grouping feature (although be warned, you do need to sort your output first).

Get-Service | Sort-Object Status | Format-Table -AutoSize -GroupBy Status

I should point out that Format-Table has a lot of features that let you do things like name the columns, specify the columns you want to output, you can even use custom columns and views. Both of which are way beyond the scope of this post and my current PowerShell knowledge. They do look interesting though 😁

3 thoughts on “#PowershellBasics: Expanding the column width to avoid truncation

  1. […] Kenneth Fisher supersizes the screen: […]

  2. Eric Russell says:

    Another useful (but little known) option is | Out-GridView.

  3. […] week I posted about a request to get a list of services. The first problem I ran into was expanding the column width, now I need to get the output off my screen and into a file to send the requestor. The cmdlet […]

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 6,758 other subscribers

Follow me on Twitter

Archives

ToadWorld Pro of the Month November 2013