Formatting Output in PowerShell
By: Cam Wohlfeil
Published: 2018-02-07 0000 EST
Category: Programming
Tags:
powershell
There's five useful commands when it comes to controlling your output in PowerShell (in my experience): Out-File, Export-Csv, Format-Table aka ft, Format-List aka fl, and Out-Null. These are all pretty self explanatory. Out-File saves your output to a regular text file (and does not translate it from a PowerShell object, beware). Export-Csv saves it as a CSV file, a very basic spreadsheet format (and does translate it from an object). ft will format the output as a table and display it in the console. This will choose the columns the command thinks you want if there are too many to display. fl will display all properties line by line for all results which is often far more info than you want, unless you are trying to find what properties are available. Finally, Out-Null will make sure no output (except errors) are written to the command line.
Get-Service | Out-File "C:\\services.txt"
Get-Service | Export-Csv "C:\\services.csv"
# *Name is a wildcard that will select all properties that end in Name
# -AutoSize will ensure no columns are cut short
Get-Service | ft *Name,Status -AutoSize
Get-Service | fl *Name,Status
Get-Service | Out-Null
More Info: