简体   繁体   English

Powershell,从注册表导出 Csv 安装的软件列表

[英]Powershell, Installed Software List from Registry Export Csv

been trying to get this to output to a csv.一直试图让它输出到 csv。 Works fine in of itself, but can't figure out how to do it.本身工作正常,但无法弄清楚如何做到这一点。 I guess because I'm using write-host it never actually goes in the pipeline which gives me a null pipeline input error.我猜是因为我使用的是 write-host,它实际上从未进入管道,这给了我一个空管道输入错误。 I've tried playing aroung with a few other ways to no avail.我试过用其他几种方法玩弄都无济于事。 Here is the "stock code" i'd like to manage to export into a csv:这是我想设法导出到 csv 的“股票代码”:

$InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"

foreach($obj in $InstalledSoftware)
    {write-host $obj.GetValue('DisplayName') -NoNewline; 
     write-host " - " -NoNewline; 
     write-host $obj.GetValue('DisplayVersion')}

As I said, it seems like it the pipeline remains empty so adding | export-csv正如我所说,管道似乎仍然是空的,所以添加| export-csv | export-csv doesn't work. | export-csv不起作用。 I don't even need it to write output it on screen, although it's a nice plus, so for now I'll try to reformulate it with something other than write-host.我什至不需要它来在屏幕上写输出,虽然这是一个不错的加分项,所以现在我将尝试用 write-host 以外的东西重新格式化它。

You need to create first an object with the information about the specific program and the add this information to an array.您需要首先使用有关特定程序的信息创建一个对象,然后将此信息添加到数组中。 Then you can export the array as an CSV.然后,您可以将数组导出为 CSV。 You could also export one entry at a time and append it to the csv.您也可以一次导出一个条目并将其附加到 csv。 But if find this approach cleaner:但如果发现这种方法更干净:

$InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"
$allSoftware = [System.Collections.ArrayList]@()
foreach($obj in $InstalledSoftware) {
    $software = New-Object -TypeName PSObject
    $software | Add-Member -MemberType NoteProperty -Name DisplayName -Value $obj.GetValue("DisplayName")
    $software | Add-Member -MemberType NoteProperty -Name Version -Value $obj.GetValue("DisplayVersion")
    $null = $allSoftware.Add($software)
}

$allSoftware | Export-Csv -Path "SomePath"

I reworked your code to produce something that can be fed to Export-csv.我重新编写了您的代码以生成可以输入到 Export-csv 的内容。

The following code at least produces a CSV file.以下代码至少会生成一个 CSV 文件。 It does produce several records with no data in them.它确实产生了几条没有数据的记录。 It may also have other errors.它也可能有其他错误。 But at least it will give you a handle on how to manipulate the objects.但至少它会给你一个关于如何操作对象的句柄。

$InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"


$installedSoftware | %{
    [pscustomobject]@{
        'DisplayName'     = $_.GetValue('DisplayName')
        'DisplayVersion'  = $_.GetValue('DisplayVersion')
        }} |Export-csv myfile.csv 

You may have to learn a little about pipelines, typecasting, hashtables, and Export-csv.您可能需要了解一些有关管道、类型转换、哈希表和 Export-csv 的知识。 I hope it's worth the effort.我希望这是值得的努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM