18

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..
Aaron
  • 3,135
  • 20
  • 51
  • 78
  • Something similar [here](https://stackoverflow.com/q/22311077/465053) which I found to be very helpful. – RBT Jul 13 '17 at 09:02

4 Answers4

28

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.

Kev
  • 118,037
  • 53
  • 300
  • 385
7

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()
LeeM
  • 1,118
  • 8
  • 18
  • I am outputting it in the console so it's fine also. Thanks for the tip. – Aaron Sep 18 '16 at 21:28
  • What's the reason for that? – typo Aug 13 '17 at 06:57
  • Because `Format-List` and `Format-Table` are for *formatting* output for *display* in the console. To select objects out of a pipeline, you should simply *select* them - then you're not dealing with whatever is going on under the hood with the formatting. *Formatting* the output for display on screen makes even less sense if you're exporting to CSV - the CSV is the format you want. `Format-Table` is a complete waste of time - `Select`, then `Export-CSV` does your formatting. – LeeM Aug 16 '17 at 00:52
2

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") }

Patrick Szalapski
  • 8,738
  • 11
  • 67
  • 129
1

You can also do this :

$result=yourCommandHere | Out-String
$result.Trim()

Or in a single line without using a variable :

(yourCommandHere | Out-String).Trim()
SebMa
  • 4,037
  • 29
  • 39