title: How to determine the session id on remote machine for usage with psexec -i using script/powershell/...?
body: <p>I am in need of a script or powershell command that will be able to determine the session id of a specific logged in user on remote machine, to be later used as parameter to the psexec -i execution of remote gui process on that session of that user on the remote machine.</p>
  
  <p>So far i managed to use</p>
  
  <pre><code>psexec \\remoteMachine -u user -p pswrd query session
  </code></pre>
  
  <p>to get list of sessions on the remote machine:</p>
  
  <pre><code>SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
  console                                     0  Conn    wdcon
  rdp-tcp#919       user                     1  Active  rdpwd
  rdp-tcp#916       user                     3  Active  rdpwd
  </code></pre>
  
  <p>so i guess i could somehow isolate the needed id and use it - but haven't managed to do that yet</p>
  
  <p>Any ideas?
  Maybe other - simpler ways?</p>
  
  <p>Thanks for the help.</p>