Remove blank lines in powershell output Remove blank lines in powershell output powershell powershell

Remove blank lines in powershell output


Try this instead:

($OSInfo `    | Format-List `        @{Name="OS Name";Expression={$_.Caption}},         @{Name="OS Boot Time";Expression={$_.ConvertToDateTime($_.LastBootUpTime)}},         @{Name="OS Install Date";Expression={$_.ConvertToDateTime($_.InstallDate)}}  `    | Out-String).Trim()

That'll clean up all the extraneous blank lines produced by Format-List. You may need to insert a couple of your own that you get to control.


Just to add a remark, because I see this being done all the time, even now - Format-List and Format-Table should only be used to output text in the console.

If you have objects to output as text files, CSV etc, then you simply should Select-Object to grab the objects you want rather than ft or fl. Then you can apply your Out-String if text formatting is required.

The above example should be:

($OSInfo `| Select-Object `    @{Name="OS Name";Expression={$_.Caption}},     ... `| Out-String).Trim()