views:

19

answers:

1

I am trying to verify whether a particular user exists on the local computer. The one solution that I can do is parsing the output of command Net user . But is there any better solution than this?

A: 

You can possibly use WMI with a query such as "select * from Win32_UserAccount where LocalAccount = True" or similar. If you are just looking for a specific account you can be able to restrict the WMI query more. See Win32_UserAccount. You may also just be able to use GetObject (which may be more efficient), but I don't know how to formulate that.

You can follow the template/code at http://snippets.dzone.com/posts/show/6967 (which is not relating to user accounts :-) to setup/create the WMI ActiveX object, execute the query, and enumerate the results.

Happy coding!

pst
Thanks pst. I tried it and is working. The code can be like var wmiobj = GetObject(winmgmts:\\\\.\\root\\cimv2");var query = "SELECT * FROM Win32_UserAccount Where LocalAccount = True And Name = '<name>'"; var users = wmiobj.ExecQuery(query); if users.count is zero user doesn't exist. else user exists
satya