Search code examples
powershellwindows-update

Display/Filter the output that only match the string from my list


Display/Filter the output that only match the string from my list

I want to filter or display only the data that match in my file list. Right now I just used select-string -Pattern mylistfile.txt , and the result will only display the whole line where the string was matched. How could I include all the data sets in the Output?

$Criteria = "IsIntalled=0"
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$myfilelist = C:\myfilelist.txt
$SearchResult = $Searcher.Search($Criteria).Updates 
$filteredResult = $SearchResult | select-string -Pattern $mylistfile -list 

Output: $SearchResult --- I just print upto 5 lines output

Title                           : 2019-04 Update for Windows 7 for x64-based 
                                  Systems (KB4493132)
AutoSelectOnWebSites            : False
BundledUpdates                  : System.__ComObject
CanRequireSource                : False
Categories                      : System.__ComObject

Title                           : 2019-05 Security and Quality Rollup for .NET 
                                  Framework 3.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 
                                  4.7, 4.7.1, 4.7.2, 4.8 for Windows 7 and 
                                  Server 2008 R2 for x64 (KB4499406)
AutoSelectOnWebSites            : True
BundledUpdates                  : System.__ComObject
CanRequireSource                : False
Categories                      : System.__ComObject


Title                           : Windows Malicious Software Removal Tool x64 
                                  - June 2019 (KB890830)
AutoSelectOnWebSites            : True
BundledUpdates                  : System.__ComObject
CanRequireSource                : False
Categories                      : System.__ComObject

Output: $filteredResult

Systems (KB4493132)   
Server 2008 R2 for x64 (KB4499406)

mylistfile.txt

KB4493132
KB4499406

My Expected Output -- It only shows dataset of (KB4493132) and (KB4499406)

Title                           : 2019-04 Update for Windows 7 for x64-based 
                                  Systems (KB4493132)
AutoSelectOnWebSites            : False
BundledUpdates                  : System.__ComObject
CanRequireSource                : False
Categories                      : System.__ComObject

Title                           : 2019-05 Security and Quality Rollup for .NET 
                                  Framework 3.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 
                                  4.7, 4.7.1, 4.7.2, 4.8 for Windows 7 and 
                                  Server 2008 R2 for x64 (KB4499406)
AutoSelectOnWebSites            : True
BundledUpdates                  : System.__ComObject
CanRequireSource                : False
Categories                      : System.__ComObject


Solution

  • Assuming $mylistfile was get-content myfilelist.txt, select-string turns the object piped into it into strings. Hence the name select-string, vs select-object. You could do it this way, still addressing the right property of an object, and using select-string with an array of patterns:

    # $mylistfile is 'KB4493132','KB4499406'
    $SearchResult | where { $_.title | select-string $mylistfile }