Home > Software engineering >  Powershell, Installed Software List from Registry Export Csv
Powershell, Installed Software List from Registry Export Csv

Time:04-28

been trying to get this to output to a 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. 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:

$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 doesn't work. 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.

CodePudding user response:

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. You could also export one entry at a time and append it to the 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"

CodePudding user response:

I reworked your code to produce something that can be fed to Export-csv.

The following code at least produces a csv file. 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. I hope it's worth the effort. Good luck.

  • Related