ShortCircuit
ShortCircuit

Reputation: 63

Filtering Objects entries in PowerShell

I started today to play with PowerShell and wanted to do a simple task. Read all my installed software, filter out "Microsoft related entries + null entries" and store them in JSON file.

Well, I was able to do that but I couldn't figure out the filtering part mostly because the whole script language is new to me and I couldn't successfully iterate to remove the entries I wanted.

Your help is appreciated!

$outputFile="C:\test.JSON"
$GetAppData= Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* |Select-Object DisplayName, DisplayVersion, InstallDate

    foreach ($Applications in $GetAppData.PSObject) 
    {

        foreach ($App in $Applications.Properties.Value) 
        {
         if ($App.'DisplayName' -like '*Microsoft*' -or !$App.'DisplayName' ) 
                {
                    $Applications.Value.PSObject.Properties.Remove($App)  
                }
         }
    }    
     
    $GetAppData| ConvertTo-Json -Depth 100 | Set-Content -Path $outputFile -Force

Upvotes: 0

Views: 282

Answers (2)

Santiago Squarzon
Santiago Squarzon

Reputation: 61068

For your specific question, this is how you can filter and export your $GetAppData. There are several ways of filtering an array and also there are some cmdlets / functions that can give you the same result as querying HKLM.

$outputFile="C:\test.JSON"
$GetAppData= Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* |Select-Object DisplayName, DisplayVersion, InstallDate

$GetAppData.Where({$_.DisplayName -and $_.DisplayName -notmatch 'Microsoft'})|ConvertTo-Json > $outputFile

Cheers!

Upvotes: 1

Lee_Dailey
Lee_Dailey

Reputation: 7489

a simpler way to get the info is to use the somewhat newer Get-Package cmdlet. [grin]

what the code does ...

  • sets the item pattern to exclude
    you could make this into a regex OR list to exclude more than one item.
  • gets the list of all packages
  • filters out the MSU items
  • filters out the items with a blank .Name property
  • filters out the items that match the $DoNotWant pattern
  • saves the list to the $Result collection
  • displays the list on screen

you could add other steps to sort by name, rearrange the properties, or otherwise customize the props in the final objects.

the code ...

$DoNotWant = 'microsoft'

$Result = Get-Package |
    Where-Object {
        $_.ProviderName -ne 'msu' -and
        $_.Name -and
        $_.Name -notmatch $DoNotWant
        }

$Result

truncated output ...

Name                           Version          Source                           ProviderName
----                           -------          ------                           ------------
7-Zip 19.00 (x64)              19.00                                             Programs
AutoHotkey 1.1.33.02           1.1.33.02                                         Programs
Bulk Rename Utility 3.3.2.1...                                                   Programs

[*...snip...*] 

PSFileTransfer                 5.31.0           https://www.powershellgallery... PowerShellGet
PSFramework                    1.4.150          https://www.powershellgallery... PowerShellGet
PSLog                          5.31.0           https://www.powershellgallery... PowerShellGet
PackageManagement              1.4.7            https://www.powershellgallery... PowerShellGet

Upvotes: 2

Related Questions