Get-UserSession - Parse query user results

This function parses the results of query user to provide object based results:  ComputerName, UserName, SessionName, Id, State, IdleTime and LogonTime

5 Star
16,338 times
Add to favorites
E-mail Twitter Digg Facebook
  • Idle Times
    2 Posts | Last post January 22, 2020
    • Any idea what causes some sessions to show 24692+13:29 as the current idle time?
    • This means that the user is actively doing something within the terminal session (typing, moving a mouse, saving file, ...)
  • Treating of IDs >= 100
    4 Posts | Last post June 03, 2019
    • Hello,
      it looks like I've stumbled upon an error. When the session ID reaches 100, the leading char of the ID is cut off resulting in a wrong ID (e.g. 175 -> 75)
    • Hi!
      Interesting, we never hit triple digits, guessing this messes with the output we're reading.
      Any chance you could run query user /server:someAffectedServer to get the output of this, and post it sanitized in a gist/pastebin type thing?
    • Great Script but again the issue remain same... until the user Session IDs are double digit, it is fine.. when it becomes to 3 digits (i.e. more than 100) the first digit is being skipped... could you please fix it ASAP ?
    • You just need to change one start position of a substring. If you can't figure out that part, I'm not sure you should be running scripts off the internet in the first place...
  • Alternative solution
    1 Posts | Last post December 03, 2018
    • Hello! Great work.
      Also, there is another solution here:
  • Stopped working for me
    2 Posts | Last post July 20, 2018
    • Get-UserSession worked for me for quite some time. After something changed in the environment (I’m not exactly sure what), the string manipulation code which is responsible for parsing the tmp file stopped functioning and started to truncate the sessionID.  In my opinion, the parsing logic in Get-UserSession is difficult to read and has proven unreliable. I re-wrote a large piece of the code in Get-UserSession, which is responsible for parsing the tmp file, with a single regular expression. I think it will be much more reliable and is much easier to read now. Let me know if you are interested and I'd be happy to share.
    • Hey Brad,
      Would you be so kind to share as I'm interested in trying it.  Thanks.
  • nothing happens when i run the script
    1 Posts | Last post June 21, 2017
    • Hi, sorry if this question sounds strange, but when i run the script:
      "Get-UserSession -ComputerName Server14" (where "Server14" is a name of a certain server in my network which i can resolve. even tried it with "localhost" but same result)
      i'm getting this error message:
      "Get-UserSession : The term 'Get-UserSession' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try agai
      At line:1 char:2
      +  Get-UserSession -ComputerName Server14
      +  ~~~~~~~~~~~~~~~
          + CategoryInfo          : ObjectNotFound: (Get-UserSession:String) [], CommandNotFoundException
          + FullyQualifiedErrorId : CommandNotFoundException" 
      although i made sure of course, that i am located in the directory of the script and made sure the name is correct.
      any idea why?
  • add username parametr
    1 Posts | Last post February 11, 2016
    • add one line
      insert after line number 50
      [string]$username = "",
      and modify string 87 (after add parametr username)
      $p = Start-Process -FilePath C:\windows\system32\cmd.exe -ArgumentList "/c query user /server:$computer > $tempfile" -WindowStyle hidden -passthru
      $p = Start-Process -FilePath C:\windows\system32\cmd.exe -ArgumentList "/c query user $username /server:$computer > $tempfile" -WindowStyle hidden -passthru
  • Remove Plural Noun
    2 Posts | Last post June 28, 2013
    • Great script! If I could make one small recommendation, it would be to make the noun singular vs. plural to keep in line with best practices.
    • Thanks for the heads up and for your own Get-ComputerSession script!