Yevrag35

List Installed Applications

Jan 29th, 2019
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. [OutputType([psobject])]
  3. param
  4. (
  5.     [parameter(Mandatory=$false,Position=0)]
  6.     [string[]] $ComputerName = $env:COMPUTERNAME,
  7.  
  8.     [parameter(Mandatory=$false,Position=1)]
  9. #    [SupportsWildcards()]
  10.     [string[]] $SearchFor
  11. )
  12.  
  13. $UninstallKeys = @(
  14.     "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
  15.     "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
  16. )
  17.  
  18. $list = New-Object 'System.Collections.Generic.List[object]';
  19.  
  20. foreach ($computer in $ComputerName)
  21. {
  22.  
  23.     if ($computer -eq "localhost" -or $computer -eq ".")
  24.     {
  25.         $computer = $env:COMPUTERNAME
  26.     }
  27.  
  28.     try
  29.     {
  30.         $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $computer);
  31.     }
  32.     catch [System.IO.IOException]
  33.     {
  34.         Write-Error "$($computer): Could not open the registry remotely!  Is the `"RemoteRegistry`" running`?"
  35.     }
  36.     if ($null -eq $Reg)
  37.     {
  38.         Write-Error "$($computer): Could not open the registry remotely!  Is the `"RemoteRegistry`" running`?"
  39.     }
  40.  
  41.     foreach($UninstallKey in $UninstallKeys)
  42.     {
  43.         #Drill down into the Uninstall key using the OpenSubKey Method
  44.         $Regkey = $Reg.OpenSubKey($UninstallKey)
  45.  
  46.         #Retrieve an array of string that contain all the subkey names
  47.         $Subkeys = $Regkey.GetSubKeyNames()
  48.  
  49.         foreach($key in $subkeys)
  50.         {
  51.  
  52.             $thisKey = $UninstallKey+"\\"+$key
  53.             $thisSubKey= $reg.OpenSubKey($thisKey)
  54.             $displayName = $thisSubKey.GetValue("DisplayName")
  55.             $publisher = $thisSubKey.GetValue("Publisher")
  56.  
  57.             if (-not [string]::IsNullOrEmpty($displayName) -or -not [string]::IsNullOrEmpty($publisher))
  58.             {
  59.                 $pso = New-Object -TypeName 'psobject' -Property @{
  60.                     ComputerName = $computer
  61.                     DisplayName = $displayName
  62.                     DisplayVersion = $thisSubKey.GetValue("DisplayVersion")
  63.                     InstallLocation = $thisSubKey.GetValue("InstallLocation")
  64.                     Publisher = $publisher
  65.                     UninstallString = $thisSubKey.GetValue("UninstallString")
  66.                 }
  67.                 $list.Add($pso)
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. if ($PSBoundParameters.ContainsKey("SearchFor"))
  74. {
  75.     $patterns = New-Object -TypeName 'System.Collections.Generic.List[System.Management.Automation.WildcardPattern]' -ArgumentList $SearchFor.Count
  76.     foreach ($appToSearch in $SearchFor)
  77.     {
  78.         $patterns.Add((New-Object -TypeName "System.Management.Automation.WildcardPattern" -ArgumentList $appToSearch, "IgnoreCase"))
  79.     }
  80.     foreach ($app in $list)
  81.     {
  82.         if (@($patterns | ForEach-Object { $_.IsMatch($app.DisplayName)}) -contains $true)
  83.         {
  84.             $app
  85.         }
  86.     }
  87. }
  88. else
  89. {
  90.     $list
  91. }
Add Comment
Please, Sign In to add comment