PoshCode Logo PowerShell Code Repository

List AddRemovePrograms by Joel Bennett 3 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2470"></script>download | new post

This script creates a WMI Class Win32_AddRemovePrograms (and, on 64bit systems, a Win32_AddRemovePrograms32 for 32bit apps) which are backed by the registry provider. They can then be queried to list installed apps (and versions) and perform much faster than running the same queries using the PowerShell Registry provider. Additionally, they can be used in GPO policies, etc.

  1. if(!(gwmi -list Win32_AddRemovePrograms)) {
  2.  
  3. set-content $pwd\RegProgs.mof @'
  4. #pragma namespace("\\\\.\\root\\cimv2")
  5. instance of __Win32Provider as $Instprov
  6. {
  7. Name ="RegProv" ;
  8. ClsID = "{fe9af5c0-d3b6-11ce-a5b6-00aa00680c3f}" ;
  9. };
  10. instance of __InstanceProviderRegistration
  11. {
  12. Provider =$InstProv;
  13. SupportsPut =TRUE;
  14. SupportsGet =TRUE;
  15. SupportsDelete =FALSE;
  16. SupportsEnumeration = TRUE;
  17. };
  18.  
  19. [dynamic, provider("RegProv"),
  20. ProviderClsid("{fe9af5c0-d3b6-11ce-a5b6-00aa00680c3f}"),
  21. ClassContext("local|HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")]
  22. class Win32_AddRemovePrograms
  23. {
  24. [key]
  25. string ProdID;
  26. [PropertyContext("DisplayName")]
  27. string DisplayName;
  28. [PropertyContext("Publisher")]
  29. string Publisher;
  30. [PropertyContext("DisplayVersion")]
  31. string Version;
  32. [PropertyContext("UninstallString")]
  33. string UninstallString;
  34. [PropertyContext("EstimatedSize")]
  35. string EstimatedSizeKb;
  36. [PropertyContext("InstallDate")]
  37. string InstallDate;
  38. };
  39. '@
  40.  
  41. #if((gwmi Win32_OperatingSystem -Property OSArchitecture).OSArchitecture -eq '64-bit') {
  42. #if([intptr]::size -eq 8) {
  43. if( test-path HKLM:\SOFTWARE\Wow6432Node ) {
  44. add-content $pwd\RegProgs.mof @'
  45. [dynamic, provider("RegProv"),
  46. ProviderClsid("{fe9af5c0-d3b6-11ce-a5b6-00aa00680c3f}"),
  47. ClassContext("local|HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall")]
  48. class Win32_AddRemovePrograms32
  49. {
  50. [key]
  51. string ProdID;
  52. [PropertyContext("DisplayName")]
  53. string DisplayName;
  54. [PropertyContext("Publisher")]
  55. string Publisher;
  56. [PropertyContext("DisplayVersion")]
  57. string Version;
  58. [PropertyContext("UninstallString")]
  59. string UninstallString;
  60. [PropertyContext("EstimatedSize")]
  61. string EstimatedSizeKb;
  62. [PropertyContext("InstallDate")]
  63. string InstallDate;
  64. };
  65. '@
  66. }
  67.  
  68. Write-Warning "MOF compiler will be RunAs Administrator ..."
  69. if(
  70.     (new-object Security.Principal.WindowsPrincipal ([Security.Principal.WindowsIdentity]::GetCurrent())
  71.     ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
  72. ) {
  73.    mofcomp.exe $pwd\RegProgs.mof
  74. } else {
  75.    (start-process mofcomp.exe -verb runas -Args "$pwd\RegProgs.mof" -Passthru).WaitForExit() # -class:forceupdate
  76. }
  77. }
  78.  
  79.  
  80. ## EXAMPLE:
  81. ## List installed 64 bit applications with ".NET" in the name (or 32bit on Windows 32bit)
  82. Get-WmiObject Win32_AddRemovePrograms -filter "DisplayName like '%.NET%'" | Sort-Object DisplayName | Format-Table DisplayName, Version
  83.  
  84. if( test-path HKLM:\SOFTWARE\Wow6432Node ) {
  85. ## List installed 32 bit applications with ".NET" in the name (or nothing on Windows 32bit)
  86. Get-WmiObject Win32_AddRemovePrograms32 -filter "DisplayName like '%.NET%'" | Sort-Object DisplayName | Format-Table DisplayName, Version
  87.  
  88. }
  89.  
  90.  
  91. ## NOTE: the alternative is a registry query which is much longer and more typing ...
  92. Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* DisplayName,Publisher,DisplayVersion,UninstallString,EstimatedSize,InstallDate -ea 0 |
  93.    Where-Object { $_.DisplayName -like "*.Net*" } | Sort-Object DisplayName | Format-Table DisplayName, DisplayVersion, EstimatedSize -AutoSize
  94.  
  95. ## And which performs miserably, by comparison:  
  96. # Duration Command
  97. # -------- -------
  98. # 0.23302s Get-WmiObject Win32_AddRemovePrograms32 -filter ...
  99. # 1.04610s Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* ...

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me