title: Looping through HKEY_USERS
body: <p>I have the below code - running this as Administrator it should have access to all HKEY_USERS loaded profiles. However, it fails on looking through on some with the following:</p>
  
  <p><strong>You cannot call a method on a null-valued expression.</strong></p>
  
  <p>It fails when it tries to get the values contained within the Run registry path:</p>
  
  <p><strong>$runKeyNames = $runKeySubKey.GetValueNames()</strong>   </p>
  
  <p>Any ideas - please note there are test values in Run!</p>
  
  <pre><code>$hkeyUsersHIVE = [Microsoft.Win32.RegistryHive]::Users
  
  $hkeyUsers = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($hkeyUsersHIVE, $env:COMPUTERNAME)
  $hkeyUsersSubkeys = $hkeyUsers.GetSubKeyNames()
  
  
  $hkeyUsersSubkeys | % {
      "$_"
      $runKey = "$_\software\Microsoft\Windows\CurrentVersion\Run"       
              $runKeySubKey = $hkeyUsers.OpenSubKey($runKey)
              $runKeyNames = $runKeySubKey.GetValueNames()     
                  if ($runKeyNames -match "phrase") 
                  {
                      "$ENV:computername -- $("HKEY_USERS:\$_\SOFTWARE\Microsoft\Windows\CurrentVersion\Run") -- EXISTS-- Key Found: $($runKeyNames -match "phrase")" 
                  }else{       
                      "$ENV:computername -- $("HKEY_USERS:\$_\SOFTWARE\Microsoft\Windows\CurrentVersion\Run") -- DOESN'T EXIST"  
                  }
  
      }
  </code></pre>