'How to use Microsoft.Update.Session() in powershell windows 7?

I want to export the update history(installed KB) list from Windows. To Windows 10, I found the code under blow:

$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = $Session.CreateUpdateSearcher()
$Searcher.QueryHistory(0, $Searcher.GetTotalHistoryCount()) | Export-Csv -Encoding UTF8 -Path D:\$env:computername+KB.csv -NoTypeInformation

However, I can't use it under Windows 7, because of some code error.

Does anyone know how to repair the code to run under Windows 7 ? or have any other way to get the installed KB list ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source