如何在PowerShell中向命令输出添加换行符?

时间:2009-10-28 18:45:04

标签: powershell newline

我使用PowerShell运行以下代码,以从注册表中获取添加/删除程序列表:

Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
    | ForEach-Object -Process { Write-Output $_.GetValue("DisplayName") } `
    | Out-File addrem.txt

我希望列表按每个程序的换行符分隔。我试过了:

Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
    | ForEach-Object -Process { Write-Output $_.GetValue("DisplayName") `n } `
    | out-file test.txt

Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
    | ForEach-Object {$_.GetValue("DisplayName") } `
    | Write-Host -Separator `n

Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
    | ForEach-Object -Process { $_.GetValue("DisplayName") } `
    | foreach($_) { echo $_ `n }

但是当输出到控制台时,所有这些都导致奇怪的格式化,并且当输出到文件时,每行后面都有三个方格字符。我尝试了Format-ListFormat-TableFormat-Wide没有运气。最初,我认为这样的事情会起作用:

Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
    | ForEach-Object -Process { "$_.GetValue("DisplayName") `n" }

但那只是给了我一个错误。

5 个答案:

答案 0 :(得分:82)

或者,只需将output field separator(OFS)设置为加倍换行符,然后确保在将其发送到文件时获得字符串:

$OFS = "`r`n`r`n"
"$( gci -path hklm:\software\microsoft\windows\currentversion\uninstall | 
    ForEach-Object -Process { write-output $_.GetValue('DisplayName') } )" | 
 out-file addrem.txt

请注意使用`而不是'。在我的键盘上(美国英语Qwerty布局),它位于 1 的左侧。 (从评论中移到这里 - 感谢Koen Zomers)

答案 1 :(得分:71)

尝试一下:

PS> $nl = [Environment]::NewLine
PS> gci hklm:\software\microsoft\windows\currentversion\uninstall | 
        ForEach { $_.GetValue("DisplayName") } | Where {$_} | Sort |
        Foreach {"$_$nl"} | Out-File addrem.txt -Enc ascii

它在我的addrem.txt文件中生成以下文本:

Adobe AIR

Adobe Flash Player 10 ActiveX

...

注意:在我的系统上,GetValue(“DisplayName”)为某些条目返回null,因此我将其过滤掉。顺便说一下,你接近这个:

ForEach-Object -Process { "$_.GetValue("DisplayName") `n" }

除了在字符串中,如果您需要访问变量的属性,即“计算表达式”,那么您需要使用子表达式语法,如下所示:

Foreach-Object -Process { "$($_.GetValue('DisplayName'))`r`n" }

基本上在双引号字符串中,PowerShell会扩展$_之类的变量,但它不会计算表达式,除非使用以下语法将表达式放在子表达式中:

$(`<Multiple statements can go in here`>).

答案 2 :(得分:6)

最终,你要尝试用每个EXTRA之间的空行来做一些有点混乱:)

我认为你真正想做的是使用Get-ItemProperty。如果缺少值,您会收到错误,但您可以使用-ErrorAction 0来抑制它们,或者只是将它们作为提醒。由于Registry提供程序返回了额外的属性,因此您需要使用与Get-Properties相同的属性Select-Object

然后,如果您希望每行上的每个属性都有空行,请使用Format-List(否则,请使用Format-Table每行获得一个)。

gci -path hklm:\software\microsoft\windows\currentversion\uninstall |
gp -Name DisplayName, InstallDate | 
select DisplayName, InstallDate | 
fl | out-file addrem.txt

答案 3 :(得分:5)

我认为你对上一个例子有正确的想法。您只有一个错误,因为您试图将引号放在已经引用的字符串中。这将解决它:

gci -path hklm:\software\microsoft\windows\currentversion\uninstall | ForEach-Object -Process { write-output ($_.GetValue("DisplayName") + "`n") }

编辑:Keith的$()运算符实际上创建了一个更好的语法(我总是忘记这个)。你也可以在引号内转义引号:

gci -path hklm:\software\microsoft\windows\currentversion\uninstall | ForEach-Object -Process { write-output "$($_.GetValue(`"DisplayName`"))`n" }

答案 4 :(得分:4)

我倾向于使用的选项,主要是因为它很简单而且我不必考虑,使用Write-Output如下所示。 Write-Output会为你输入一个EOL标记,你可以输出完成的字符串。

Write-Output $stringThatNeedsEOLMarker | Out-File -FilePath PathToFile -Append

或者,您也可以使用Write-Output构建整个字符串,然后将完成的字符串推送到Out-File