Delete blank lines in PowerShell mode

I am trying to remove blank lines before and after the output, but it just doesn't work. I tried adding -NoNewLine after the very first Write-Host, but so far it has only deleted one empty line.

The 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.. 
+7
output powershell
source share
2 answers

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() 

This will clear all extraneous empty lines created by Format-List . You may need to insert a couple of your own that you can control.

+10
source share

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

If you have objects to output as text files, CSV, etc., you just need Select-Object to capture the required objects, not ft or fl . You can then 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() 
+3
source share

All Articles