Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

powershell - Remove blank lines from output?

When I run this command, it returns the output with lots of blank lines.

Get-Host | Select Name, Version | Format-List

I tried using ExpandProperty but it still leaves me with one blank line at the end of the output, is there a way to remove the last blank line?

Get-Host | Select -ExpandProperty Name, Version | Format-List 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you really need the format that Format-List provides, then you can use Out-String and Trim() to get rid of empty lines:

Get-Host | Select Name,Version | Format-List | Out-String | ForEach-Object { $_.Trim() }

But, depending on your needs, it's probably easier to just use CSV or the JSON format for further processing (see ConvertTo-Json or ConvertTo-Csv).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...