0

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
2
  • are you trying to use, !$App.'DisplayName', as a means of it being null/empty? Commented Mar 9, 2021 at 19:56
  • @AbrahamZinala Yes Commented Mar 9, 2021 at 20:25

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

This Works! Thank you very much. silly question: How was $Result linked to the $GetAppData without being mentioned in the code.
Answering my own question: Get-Package. :D
@ShortCircuit - you are most welcome! glad to have helped a bit. ///// yep ... almost any PoSh code block can be assigned to a $Var. it's the recommended way to save things to an array since it avoids the nasty gotcha when using += on an array ... arrays are fixed size. [grin]
1

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!

1 Comment

Also an acceptable solution. Thank you for the assistance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.