Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


December 11, 2006

List Locally Logged-on Users

A handy JScript script outputs user info in a form other scripts can easily use
RSS
View this exclusive article with VIP access -- click here to join |
See More Jscript Articles Here | Reprints | Or sign up for our VIP Monthly Pass!

It's useful to be able to query a computer for the users that are logged on locally. You can do this with Sysinternals' free PsLoggedOn utility, but sometimes you want to use the information directly in a scripted task (e.g., contacting users who are logged on to a terminal server that needs a reboot because of a security patch). Although it's possible to execute the PsLoggedOn utility and parse its output in a script for use in other scripts, I decided to see whether I could gather the names of logged-on users by using a script instead of relying on PsLogged-On.

How PsLoggedOn Works
I started by downloading and examining the C source code for the Ps-LoggedOn utility to see how it determines locally logged-on users. The technique it uses isn't complex and is fairly easy to implement by calling the methods in Windows Management Instrumentation's (WMI's) StdRegProv class. An overview of the technique PsLoggedOn uses is as follows:

  1. Enumerate the subkeys in the computer's HKEY_USERS registry subtree.
  2. For each subkey in the subtree that contains a SID value, determine whether the subkey contains a Volatile Environment subkey. If it does, and if the Volatile Environment subkey contains one or more values, then the user that the SID represents is currently logged on.
  3. Convert the SID value into the corresponding username.

In Figure 1, the HKEY _USERS subtree is expanded to show a subkey that has a SID value. You can also see that the subkey contains a Volatile Environment subkey that contains values. Thus, you know that the user represented by the SID S-1-5-21-299502267-1078145449-725345543-500 is currently logged on. . . .

Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

How can I stop and start services from the command line?

...

Microsoft Touts Xbox 360 'Black Friday' Success

Microsoft this week revealed that its Xbox 360 video game console outsold its Sony PlayStation 3 rival 3-to-1 on "Black Friday," which curiously describes the three day weekend after Thanksgiving, a period that unofficially kicks off the holiday selling ...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing