locked
Active Directory users listing RRS feed

  • Question

  • Hi

    I'm new to C#,I want to write an application to list out the password expiry date of a one particular group users in Active Directory. 

    I've Windows 10 OS, Visual Studio 2005 & Dot Net Framework 4.0 and Active Directory is  Windows Server 2008 R2. 

    Can anyone suggest some code and tips please. 

    Regards

    Venkat Nori


    nories

    Wednesday, August 19, 2020 7:49 PM

Answers

  • Hi Timon

    Thanks & sorry for delayed reply.  Your link has helped me a lot. 

    I've modified as per the requirement and below code worked for me. 

    Thanks Once Again.

     public static void GetListOfAdUsersByGroup(string domainName, string groupName)
            {
                DirectoryEntry entry = new DirectoryEntry("LDAP://DC=" + domainName + ",DC=LAN");

                DirectorySearcher search = new DirectorySearcher(entry);
                string query = "(&(objectCategory=person)(objectClass=user)(memberOf=*))";
                search.Filter = query;
                search.PropertiesToLoad.Add("memberOf");
                search.PropertiesToLoad.Add("name");
                search.PropertiesToLoad.Add("mail");
                search.PropertiesToLoad.Add("pwdLastSet");

                System.DirectoryServices.SearchResultCollection mySearchResultColl = search.FindAll();
                Console.WriteLine("Members of the {0} Group in the {1} Domain", groupName, domainName);
                foreach (SearchResult result in mySearchResultColl)
                {
                     
                    Console.WriteLine("Name= " + result.Properties["name"][0] + " : Mail=" + result.Properties["mail"][0] + " : Password Last Set=" + DateTime.FromFileTime((Int64)(result.Properties["pwdLastSet"][0])));         

      
                }
            }


    nories

    Friday, August 21, 2020 6:05 AM

All replies

  • Hi Venkata,

    Thank you for posting here.

    This link provides a way to get all users in a group:

    get all users from a group in Active Directory

    After that, you should be able to get the expiration time through the AccountExpirationDate property.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by CoolDadTx Thursday, August 20, 2020 1:40 PM
    Thursday, August 20, 2020 8:13 AM
  • Hi Timon

    Thanks & sorry for delayed reply.  Your link has helped me a lot. 

    I've modified as per the requirement and below code worked for me. 

    Thanks Once Again.

     public static void GetListOfAdUsersByGroup(string domainName, string groupName)
            {
                DirectoryEntry entry = new DirectoryEntry("LDAP://DC=" + domainName + ",DC=LAN");

                DirectorySearcher search = new DirectorySearcher(entry);
                string query = "(&(objectCategory=person)(objectClass=user)(memberOf=*))";
                search.Filter = query;
                search.PropertiesToLoad.Add("memberOf");
                search.PropertiesToLoad.Add("name");
                search.PropertiesToLoad.Add("mail");
                search.PropertiesToLoad.Add("pwdLastSet");

                System.DirectoryServices.SearchResultCollection mySearchResultColl = search.FindAll();
                Console.WriteLine("Members of the {0} Group in the {1} Domain", groupName, domainName);
                foreach (SearchResult result in mySearchResultColl)
                {
                     
                    Console.WriteLine("Name= " + result.Properties["name"][0] + " : Mail=" + result.Properties["mail"][0] + " : Password Last Set=" + DateTime.FromFileTime((Int64)(result.Properties["pwdLastSet"][0])));         

      
                }
            }


    nories

    Friday, August 21, 2020 6:05 AM