title: Powershell compare array to check for GUID
body: <p>I am creating a PowerShell script to check the local system for a predefined list of <code>GUID</code> (IdentifyingNumber) which belong to JRE.  The purpose is to remove any old versions that are part of the list before installing the latest version of Java.</p>
  
  <p>The script appears to run correctly for matching the <code>GUID</code> but when I run  <code>msiexec.exe /x</code> within my for loop and if statement it only runs one time before the script completes.  It should remove each of the old versions that are found.</p>
  
  <p>If anyone could help explain what it is I am missing here it would be greatly appreciated!</p>
  
  
  
  <pre class="lang-powershell prettyprint-override"><code>[string[]] $Java_GUIDS = @(
  #Java Auto Updater    
  "{4A03706F-666A-4037-7777-5F2748764D10}",
  #Java 8 Update 31 - 64-bit                                                   
  "{26A24AE4-039D-4CA4-87B4-2F86418031F0}",
  #Java 8 Update 31 - 32-bit                                                              
  "{26A24AE4-039D-4CA4-87B4-2F83218031F0}",
  #Java 8 Update 25 - 64-bit
  "{26A24AE4-039D-4CA4-87B4-2F83218025F0}",
  #Java 8 Update 20 - 32-bit
  "{26A24AE4-039D-4CA4-87B4-2F83218020F0}",
  #Java 8 Update 20 - 64-bit
  "{26A24AE4-039D-4CA4-87B4-2F83218020FF}",
  # ARRAY DEFINITION TRUNCATED
  #Java 5 Update 22
  "{3248F0A8-6813-11D6-A77B-00B0D0150220}",
  #Java 5 Update 23
  "{3248F0A8-6813-11D6-A77B-00B0D0150230}")
  
  #Show currently installed Java Version(s)
  Write-Host "Currently Installed Java Versions:"
  gwmi win32_product -filter "name like 'Java%'" | select name,IdentifyingNumber,Version
  
  [string[]] $Installed = (gwmi win32_product -filter "name like 'Java%'" | Select-Object -expand IdentifyingNumber)
  
  foreach($Installed_GUID in $Installed)
  {
      if ($Java_GUIDS -contains $Installed_GUID)
      {
          Write-Host "Found $Installed_GUID in Java GUIDS Array.  Removing..."
          msiexec.exe /x $Installed_GUID /qn /norestart /L*V "C:\Windows\TEMP\Java Removal.log"
          Write-Host "Done"
      }
  else
      {
          Write-Host "Did NOT find $Installed_GUID in Java GUIDS Array"
      }
  }
  Write-Host "Complete"
  </code></pre>
  
  
  
  <p>Below are three consecutive runs of the script which show that only one version each run is being uninstalled successfully.</p>
  
  <pre class="lang-powershell prettyprint-override"><code>PS C:\Users\srj3&gt; C:\Users\srj3\Desktop\Java_Removal.ps1
  Currently Installed Java Versions:
  
  name                                                                                    IdentifyingNumber                                                                       Version                                                                                
  ----                                                                                    -----------------                                                                       -------                                                                                
  Java 7 Update 75                                                                        {26A24AE4-039D-4CA4-87B4-2F03217075FF}                                                  7.0.750                                                                                
  Java 8 Update 31                                                                        {26A24AE4-039D-4CA4-87B4-2F83218031F0}                                                  8.0.310                                                                                
  Java 8 Update 25                                                                        {26A24AE4-039D-4CA4-87B4-2F83218025F0}                                                  8.0.250                                                                                
  Java 7 Update 75 (64-bit)                                                               {26A24AE4-039D-4CA4-87B4-2F06417075FF}                                                  7.0.750                                                                                
  Java 8 Update 31 (64-bit)                                                               {26A24AE4-039D-4CA4-87B4-2F86418031F0}                                                  8.0.310                                                                                
  Java Auto Updater                                                                       {4A03706F-666A-4037-7777-5F2748764D10}                                                  2.8.31.13                                                                              
  Found {26A24AE4-039D-4CA4-87B4-2F03217075FF} in Java GUIDS Array.  Removing...
  Done
  Found {26A24AE4-039D-4CA4-87B4-2F83218031F0} in Java GUIDS Array.  Removing...
  Done
  Found {26A24AE4-039D-4CA4-87B4-2F83218025F0} in Java GUIDS Array.  Removing...
  Done
  Found {26A24AE4-039D-4CA4-87B4-2F06417075FF} in Java GUIDS Array.  Removing...
  Done
  Found {26A24AE4-039D-4CA4-87B4-2F86418031F0} in Java GUIDS Array.  Removing...
  Done
  Found {4A03706F-666A-4037-7777-5F2748764D10} in Java GUIDS Array.  Removing...
  Done
  Complete
  
  
  
  PS C:\Users\srj3&gt; C:\Users\srj3\Desktop\Java_Removal.ps1
  Currently Installed Java Versions:
  
  name                                                                                    IdentifyingNumber                                                                       Version                                                                                
  ----                                                                                    -----------------                                                                       -------                                                                                
  Java 8 Update 31                                                                        {26A24AE4-039D-4CA4-87B4-2F83218031F0}                                                  8.0.310                                                                                
  Java 8 Update 25                                                                        {26A24AE4-039D-4CA4-87B4-2F83218025F0}                                                  8.0.250                                                                                
  Java 7 Update 75 (64-bit)                                                               {26A24AE4-039D-4CA4-87B4-2F06417075FF}                                                  7.0.750                                                                                
  Java 8 Update 31 (64-bit)                                                               {26A24AE4-039D-4CA4-87B4-2F86418031F0}                                                  8.0.310                                                                                
  Java Auto Updater                                                                       {4A03706F-666A-4037-7777-5F2748764D10}                                                  2.8.31.13                                                                              
  Found {26A24AE4-039D-4CA4-87B4-2F83218031F0} in Java GUIDS Array.  Removing...
  Done
  Found {26A24AE4-039D-4CA4-87B4-2F83218025F0} in Java GUIDS Array.  Removing...
  Done
  Found {26A24AE4-039D-4CA4-87B4-2F06417075FF} in Java GUIDS Array.  Removing...
  Done
  Found {26A24AE4-039D-4CA4-87B4-2F86418031F0} in Java GUIDS Array.  Removing...
  Done
  Found {4A03706F-666A-4037-7777-5F2748764D10} in Java GUIDS Array.  Removing...
  Done
  Complete
  
  
  
  PS C:\Users\srj3&gt; C:\Users\srj3\Desktop\Java_Removal.ps1
  Currently Installed Java Versions:
  
  name                                                                                    IdentifyingNumber                                                                       Version                                                                                
  ----                                                                                    -----------------                                                                       -------                                                                                
  Java 8 Update 25                                                                        {26A24AE4-039D-4CA4-87B4-2F83218025F0}                                                  8.0.250                                                                                
  Java 7 Update 75 (64-bit)                                                               {26A24AE4-039D-4CA4-87B4-2F06417075FF}                                                  7.0.750                                                                                
  Java 8 Update 31 (64-bit)                                                               {26A24AE4-039D-4CA4-87B4-2F86418031F0}                                                  8.0.310                                                                                
  Java Auto Updater                                                                       {4A03706F-666A-4037-7777-5F2748764D10}                                                  2.8.31.13                                                                              
  Found {26A24AE4-039D-4CA4-87B4-2F83218025F0} in Java GUIDS Array.  Removing...
  Done
  Found {26A24AE4-039D-4CA4-87B4-2F06417075FF} in Java GUIDS Array.  Removing...
  Done
  Found {26A24AE4-039D-4CA4-87B4-2F86418031F0} in Java GUIDS Array.  Removing...
  Done
  Found {4A03706F-666A-4037-7777-5F2748764D10} in Java GUIDS Array.  Removing...
  Done
  Complete
  </code></pre>