作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 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-List
、Format-Table
和 Format-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/
我是一名优秀的程序员,十分优秀!