Scroll Top
Evotec Services sp. z o.o., ul. Drozdów 6, Mikołów, 43-190, Poland

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

Format-Table

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.

Posty powiązane