gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:33:08 25 4
gpt4 key购买 nike

我使用 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" }

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

最佳答案

或者,只需设置 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)

关于powershell - 如何在 PowerShell 中向命令输出添加换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1639291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com