PowerShell

Does having Format-Table or Format-List in the middle of the pipeline makes sense?

Recently there was this discussion on PowerShell Group on Facebook about issue posted by one user. While the subject doesn't matter for this post, few people commented on the idea that he was using Format-Table in the middle of the pipeline to do something. They all said Format-Table should be used as last in the pipeline. Otherwise, you would get the wrong output. But should it? Well, it depends!

Use case for Format-Table / Format-List

Your standard use case for Format-Table or Format-List would be something like this

Get-Process | Format-Table -Autosize

But what if you would like to have that data exported to file? Well, it depends. If you want further use the data in a way that can be imported back to PowerShell, to Excel, or have some kind of easy processing later on you should use proper commands.

# Exports to XML
Get-Process | Export-CliXml -LiteralPath $Env:USERPROFILE\Desktop\MyFile.xml
# Exports to CSV
Get-Process | Export-Csv -LiteralPath $Env:USERPROFILE\Desktop\MyCsv.csv
# Export to Excel (using PSWriteExcel module)
Get-Process | ConvertTo-Excel -FilePath $env:USERPROFILE\Desktop\MyExcel.xlsx -WorksheetName 'MyName'
# Export to HTML (using PSWriteHTML module)
Get-Process | Out-HtmlView

And all those commands will give you file output that will be in a form that you can do something with it.

Format-Table / Format-List in the middle of the pipeline?

But if you wanted to use Format-Table and have that output in a file? While it may not be obvious, it does work.

Get-Process | Format-Table -AutoSize | Add-Content -LiteralPath $Env:USERPROFILE\Desktop\MyOtherOutput.txt

Get-Process | Format-Table -AutoSize | Set-Content -LiteralPath $Env:USERPROFILE\Desktop\MyOtherOutput1.txt

Ok, so it's not what we expected right? And that's why people are saying to use Format-Table, Format-List as last in the pipeline because it's supposed to be for display purposes only. How about this?

Get-Process | Format-Table -AutoSize | Out-File -LiteralPath $Env:USERPROFILE\Desktop\MyOutput.txt

So, it seems Out-File is able to deliver what you see on the screen into a file! But that's not all. What if we wanted to use Set-Content and Add-Content the same way we use Out-File? Take a look at this

# See output
Get-Process | Format-Table -AutoSize | Out-String

# Export to file 
Get-Process | Format-Table -AutoSize | Out-String | Set-Content -LiteralPath $Env:USERPROFILE\Desktop\MyOtherOutput1.txt

See, we merely introduced Out-String which gives me the same output as you would get with Out-File. So, can we use Format-Table/Format-List in the middle of the pipeline? Yes, we can! But the output will not be an object that we can quickly process later on. Still, if we just wanted to see the same thing we have on screen in the form of a file, why not use it that way? There are a couple of other ways to use Out-String and Format-Table, but that's something for another blog post.

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

2 weeks ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

5 months ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

7 months ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

8 months ago

How to Efficiently Remove Comments from Your PowerShell Script

As part of my daily development, I create lots of code that I subsequently comment…

9 months ago

Unlocking PowerShell Magic: Different Approach to Creating ‘Empty’ PSCustomObjects

Today I saw an article from Christian Ritter, "PowerShell: Creating an "empty" PSCustomObject" on X…

9 months ago