Retrieve a List of All Windows Accounts or Groups

Problem

You need to retrieve a list of all users on the current computer or on a network domain.

Solution

Perform a query using the Win32_UserAccount or Win32_Group Windows Management Instrumentation (WMI) class.

Discussion

The Microsoft .NET Framework doesn’t include any classes that allow you to retrieve a list of Windows accounts. However, there’s no need to delve into the intricacies of the Win32 API—instead, you can access this information by performing a simple WMI query.

To use the WMI objects, you must import the System.Management namespace and add a reference to the System.Management.dll assembly. You can then retrieve a list of users by domain, by computer name, or by using a combination of ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.