# 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!

blog

24 Apr 2019 pl

![Does having Format-Table or Format-List in the middle of the pipeline makes sense?](/wp-content/uploads/2019/04/img_5cc0b9be73559.png)

[Zobacz obrazy (5)](#pf-content-image-1-0)

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

```powershell
Get-Process | Format-Table -Autosize
```

[!\[Konsola PowerShell pokazująca wynik Get-Process sformatowany jako tabela\](/wp-content/uploads/2019/04/img_5cc0b9be73559.png)](/wp-content/uploads/2019/04/img_5cc0b9be73559.png)

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.

```powershell
# 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.

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

[!\[Plik tekstowy utworzony po przekazaniu Format-Table do Add-Content\](/wp-content/uploads/2019/04/img_5cc0bce948305.png)](/wp-content/uploads/2019/04/img_5cc0bce948305.png)

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

[!\[Plik tekstowy utworzony po przekazaniu Format-Table do Set-Content\](/wp-content/uploads/2019/04/img_5cc0bcd1e7fb6.png)](/wp-content/uploads/2019/04/img_5cc0bcd1e7fb6.png)

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?

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

[!\[Sformatowana tabela procesów zachowana po przekazaniu Format-Table do Out-File\](/wp-content/uploads/2019/04/img_5cc0bd48a9b4d.png)](/wp-content/uploads/2019/04/img_5cc0bd48a9b4d.png)

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

```powershell
# 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
```

[!\[Sformatowana tabela procesów zachowana po użyciu Out-String i Set-Content\](/wp-content/uploads/2019/04/img_5cc0bd7d68bbc.png)](/wp-content/uploads/2019/04/img_5cc0bd7d68bbc.png)

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.

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:3BC1AD9205271EEA2CAABFD96589BD4D6C74956BDFD706B878AE19616DC6E9A1 -->
