query session command gives error code 5 even when AllowRemoteRPC is set to 1

Solution 1:

I think the problem is in the UAC remote restrictions.

Do this on the target computer:

  • Run regedit
  • Navigate to the following registry subkey:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System
    
  • If an entry named LocalAccountTokenFilterPolicy registry entry does not exist, create it as DWORD

  • Double-click LocalAccountTokenFilterPolicy and set its value to 1.

Reference:

Microsoft's Description of User Account Control and remote restrictions in Windows Vista.

Solution 2:

Rather than changing registry values, you could always use PSEXEC from a command prompt or Invoke-Command from a PowerShell prompt to execute QWINSTA locally. Both of these will require you have administrative rights on the remote machine (which means opening the command prompt under other credentials, including the credentials as PSEXEC switches or, in the case of PowerShell, including -Credential (Get-Credential) in the command.

Command prompt example:

PSEXEC \\MYPC cmd /c "qwinsta /server:localhost"

PowerShell example:

Invoke-Command -ComputerName MyPC -ScriptBlock { qwinsta /server:localhost }