Blog Post

#PowershellBasics: Expanding the column width to avoid truncation

,

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 😁

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

Share

Share

Rate