In windows I can get the remote desktop sessions on a particular server using:

qwinsta /server:SERVER_NAME

But this gives me sessions of all different states(e.g.: Active, Disc, ... ).

Does any body know about a command or batchfile or script to just get the "active" sessions? I want to be able to run it from an ordinary Windows 7 client.


qwinsta /server:SERVER_NAME | find /I "Active"

Note that this is just a quick&dirty one-liner which would just spit out every line containing "Active" (in lower or upper case). If you expect user names which contain this string, things are going to be more complicated and require to check the fourth column for the correct "Active" string:

for /F "usebackq tokens=1,2,3,4,5*" %i in (`qwinsta /server:SERVER_NAME ^| find "Active"`) do if "%l" == "Active" ( echo %i %j %k %l %m )

If you need to process single fields of the output anyway, the latter form is preferred as it would expose the fields in the %i ... %m variables.

But this construct also breaks if you have user names containing spaces. If this is the case, all is lost with qwinsta and you should be looking for a PowerShell function to retreive the user list instead.

  • 1
    Thanks synecticon-dj. For those who like me are not much into scripting I just searched and found out "/I" is to ignore case, so qwinsta /server:SERVER_NAME | find "Active" just works as well. – hhsaffar May 27 '13 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.