How to list all Windows Users

I had to figure out how to write all currently constructed Windows Users to the screen and managed it with this little piece of C# code:

using System.Management;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SelectQuery query = new SelectQuery("Win32_UserAccount");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
            foreach (ManagementObject envVar in searcher.Get())
            {
                Console.WriteLine("Username : {0}", envVar["Name"]);
            }
            
            Console.ReadLine();

        }
    }
}

Depending on the PC it might eat up a little more time sometimes.

6 comments ↓

#1 raju on 10.10.07 at 10:25 am

Thanks for the code!

#2 merawa on 03.22.08 at 9:42 pm

thank you very much,
but i have a problem, why when i change the name of ana account the running result is still the same, the result not update? if the account name = student and i change it to teacher the result stay as the first run (student) how can i solve this problem, please tell me

#3 veryhappyperson on 02.17.09 at 4:05 am

You are awesome! I’ve been trying to get netenumuser to work but this is 1000% easier 🙂 Many, many thanks to you.

#4 mcpd on 06.18.09 at 9:45 am

Thanks, thanks for the code!

#5 Marin on 11.03.10 at 2:25 pm

Wow!
Just two lines of code on how to list all Windows Users! I wrote a 20-line function that did the same… (and, not to mention – it wasn’t really accurate!) 🙂

Thanks!

#6 Thibault on 07.03.12 at 3:20 pm

Great article ! It works.
Thank you !

Leave a Comment