删除Powershell输出中的空白行
问题描述:
我正在尝试删除输出前后的空白行,但它不起作用.我尝试在第一个Write-Host之后添加-NoNewLine,但是到目前为止只删除了一个空白行.
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.
代码:
$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.."
输出:
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..
我想做什么:
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..
答
尝试以下方法:
($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()
这将清除Format-List
产生的所有多余的空白行.您可能需要插入一些自己要控制的东西.
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.