Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove blank lines in powershell output

I'm trying to remove blank lines before and after output but it's just not working. I tried Adding -NoNewLine after the very first Write-Host, but that only removes one blank line so far.

Code:

  $tag1 = "c91638"

    Write-Host "Operating System Information"


    $OSInfo = get-wmiobject -class win32_operatingsystem -computername $tag1 

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

    Write-Host "Line test.."

Outputs:

Operating System Information


OS Name         : Microsoft Windows 7 Enterprise 
OS Boot Time    : 8/27/2015 2:05:35 AM
OS Install Date : 4/4/2014 11:39:15 AM



Line test..

What I want to do:

Operating System Information

OS Name         : Microsoft Windows 7 Enterprise 
OS Boot Time    : 8/27/2015 2:05:35 AM
OS Install Date : 4/4/2014 11:39:15 AM

Line test..
like image 421
Aaron Avatar asked Aug 27 '15 14:08

Aaron


People also ask

How do you remove blank lines in a text?

Open TextPad and the file you want to edit. Click Search and then Replace. In the Replace window, in the Find what section, type ^\n (caret, backslash 'n') and leave the Replace with section blank, unless you want to replace a blank line with other text. Check the Regular Expression box.

How do I get full output in PowerShell?

All you have to do is to go to Out-String and add the -Width parameter. Keep in mind that the -Width parameter of Out-File cmdlet specifies the number of characters in each line of output. Any other characters will simply be truncated, not wrapped.

How do I leave a blank line in PowerShell?

There are different ways to add newline to string or variable in PowerShell using carriage return `r or line continuation character ` at the end of code. Backtick (`) character is the PowerShell line continuation character. It ensures the PowerShell script continues to a new line.


2 Answers

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()
like image 82
LeeM Avatar answered Oct 04 '22 13:10

LeeM


Using .Trim() goes too far in that it deletes leading & trailing spaces as well as blank lines. If you want to delete only blank lines, try this, after any code that results in a string, do:

$result.Trim("`r","`n")

Or for many strings, e.g. after Format-Table or similar:

$results | Format-Table | Out-String | ForEach-Object { $_.Trim("`r","`n") }

like image 22
Patrick Szalapski Avatar answered Oct 04 '22 13:10

Patrick Szalapski