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

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

[Voir les images (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
```

[!\[PowerShell console showing Get-Process formatted as a table\](/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
```

[!\[Text file output produced by piping Format-Table into 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
```

[!\[Text file output produced by piping Format-Table into 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
```

[!\[Formatted process table preserved when piping Format-Table into 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
```

[!\[Formatted process table preserved after Out-String and 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.

## Projets associés

Projets liés aux tags, catégories et au contenu de cet article.

[Tous les projets](/fr/projets/)

Recherche des projets associés...

[Retour au blog](/blog)

## Articles associés

- [Check an API write after a PowerShell timeout](/fr/blog/verify-uncertain-api-outcomes-powershell/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/fr/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)
- [ImagePlayground: image automation that stays in PowerShell](/fr/blog/imageplayground-powershell-image-automation/)

<!-- PowerForge markdown artifact sha256:1E9BA5217779F431223B063756A6F1E8596034E99A40F67A854C23780E53E749 -->
