satya
satya

Reputation: 2607

How to Check if an user account exists or not (on local machine) in windows?

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?

Upvotes: 2

Views: 2788

Answers (1)

user166390
user166390

Reputation:

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!

Upvotes: 1

Related Questions