0

How can I get a list of Active Directory Users (Only the Users that appear in the windows Logon Screen)

I have tried many methods using Windows Principle library & WMI Select commands. I keep getting Admministrator, Guest & some other VUSRNEIL-DELL. Neither of these 3 user accounts appear on the Logon screen. How can I Deifferientiate between these user types?

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Neil Hobson
  • 515
  • 1
  • 7
  • 14

2 Answers2

1
//Add a reference on System.DirectoryServices.dll
    using System.DirectoryServices;    
    //Connect to your LDAP
    DirectoryEntry Ldap = new DirectoryEntry("LDAP://ADName", "Login", "Password");
    DirectorySearcher searcher = new DirectorySearcher(Ldap);
    //specify that you search user only by filtering AD objects
    searcher.Filter = "(objectClass=user)";
    //Loop on each users
     foreach( SearchResult result in searcher.FindAll() )
        {
           // On récupère l'entrée trouvée lors de la recherche
           DirectoryEntry DirEntry = result.GetDirectoryEntry();

           //On peut maintenant afficher les informations désirées
           Console.WriteLine("Login : " + DirEntry.Properties["SAMAccountName"].Value);
           Console.WriteLine("FirstName: " + DirEntry.Properties["sn"].Value);
           Console.WriteLine("LastName: " + DirEntry.Properties["givenName"].Value);
           Console.WriteLine("Email : " + DirEntry.Properties["mail"].Value);
           Console.WriteLine("Phone: " + DirEntry.Properties["TelephoneNumber"].Value);
           Console.WriteLine("Description : " + DirEntry.Properties["description"].Value);

           Console.WriteLine("-------------------");
        }
Hassan Boutougha
  • 3,871
  • 1
  • 17
  • 17
  • That's going to give you **all** users from the entire domain - even just enumerating those could take quite a long time ... – marc_s Aug 25 '12 at 21:08
  • I'm not wanting users on a Domain. I just want users on a Local machhine. In my case I have 2 Users (not on a domain) that appear on the logon screen at startup. I only want those 2 accounts returned from my method (without Administrator, Guest...etc) – Neil Hobson Aug 27 '12 at 12:33
0

Check Win32_LogonSession and Win32_LoggedOnUser classes (where Win32_LogonSession.LogonType='2') for the current logged on user that you can then relate back to the Win32_Account class ;)

MacG
  • 271
  • 2
  • 4