title: Run Reg.exe in PS script will not work on remote machine ERROR: Invalid key name
body: <p>If I run the command from with in the script</p>
  
  <blockquote>
  <pre><code>reg add "HKU\$sid\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v AutoConfigURL /t REG_SZ /d "http://mydomaon.com/proxies/proxy.pac" /f
  </code></pre>
  </blockquote>
  
  <p>it works perfectly, because it is local.</p>
  
  <p>When I try to change the same registry key on a remote machine and changing the line to this</p>
  
  <blockquote>
  <pre><code>reg add "\\$machinename\HKU\$sid\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v AutoConfigURL /t REG_SZ /d "http://mydomaon.com/proxies/proxy.pac" /f
  </code></pre>
  </blockquote>
  
  <p>It fails with 
  <strong>ERROR: reg : ERROR: Invalid key name. + CategoryInfo          : NotSpecified: (ERROR: Invalid key name.:String) [], RemoteException
  ERROR:     + FullyQualifiedErrorId : NativeCommandError</strong></p>
  
  <pre><code>ForEach ($machine in $machines)
  {
  
      $User = New-Object System.Security.Principal.NTAccount($env:UserName)
      $sid = $User.Translate([System.Security.Principal.SecurityIdentifier]).value
      New-PSDrive HKU Registry HKEY_USERS
  #       Get-Item "HKU:\${sid}"
      Set-Location HKU:\
      cd HKU:\
      reg add '$SID\Software\Microsoft\Windows\CurrentVersion\Internet Settings' /v AutoConfigURL /t REG_SZ /d 'http://proxy.domain.com/proxies/proxy.pac' /f
  
  }
  </code></pre>