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

mik*_*ike 65 powershell newline

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

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

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

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 }
Run Code Online (Sandbox Code Playgroud)

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

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

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

Jay*_*kul 84

或者,只需将输出字段分隔符(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
Run Code Online (Sandbox Code Playgroud)

当心使用`而不是'.在我的键盘上(美英格式Qwerty布局),它位于左侧1.
(从评论中移到这里 - 感谢Koen Zomers)

  • 效果很好.小心使用`而不是'.在我的键盘上(美英格式Qwerty布局),它位于1的左侧. (7认同)

Kei*_*ill 77

尝试一下:

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
Run Code Online (Sandbox Code Playgroud)

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

Adobe AIR

Adobe Flash Player 10 ActiveX

...
Run Code Online (Sandbox Code Playgroud)

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

ForEach-Object -Process { "$_.GetValue("DisplayName") `n" }
Run Code Online (Sandbox Code Playgroud)

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

Foreach-Object -Process { "$($_.GetValue('DisplayName'))`r`n" }
Run Code Online (Sandbox Code Playgroud)

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

$(`<Multiple statements can go in here`>).
Run Code Online (Sandbox Code Playgroud)


Jay*_*kul 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
Run Code Online (Sandbox Code Playgroud)


Lud*_*ant 5

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

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

编辑:Keith的$()运算符实际上创建了一个更好的语法(我总是忘记这个).您还可以在引号内转义引号,如下所示:

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


小智 5

我倾向于使用的选项,主要是因为它很简单而且我不必考虑,使用如下写输出。Write-Output 将为您在字符串中放置一个 EOL 标记,您可以简单地输出完成的字符串。

Write-Output $stringThatNeedsEOLMarker | Out-File -FilePath PathToFile -Append
Run Code Online (Sandbox Code Playgroud)

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