我使用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" }
但这给了我一个错误。
发布于 2009-10-29 05:16:29
我认为你上一个例子的想法是正确的。您只得到一个错误,因为您试图将引号放入已引用的字符串中。这将修复它:
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" }
发布于 2009-10-29 21:24:10
最后,您尝试处理每个空行之间的额外空行会让人感到有点困惑:)
我认为你真正想做的是使用Get-ItemProperty。当值丢失时,您会得到错误,但是您可以使用-ErrorAction 0
禁止它们,或者只是将它们留作提醒。因为注册表提供程序返回额外的属性,所以您需要使用与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
发布于 2013-02-22 02:00:40
我倾向于使用的选项,主要是因为它很简单,我不需要思考,使用Write-Output,如下所示。Write-Output将在字符串中放置一个EOL标记,您可以简单地输出完成的字符串。
Write-Output $stringThatNeedsEOLMarker | Out-File -FilePath PathToFile -Append
或者,也可以使用Write-Output构建整个字符串,然后将完成的字符串推送到Out-File中。
https://stackoverflow.com/questions/1639291
复制相似问题