Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6752609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:03:53+00:00 2026-05-26T13:03:53+00:00

Is there any sample that deletes computer account from AD using C#? I have

  • 0

Is there any sample that deletes computer account from AD using C#?

I have searched many sources, but all are about user account.

added my code here, i always got errors for some reason.

public static bool checkExistingPC(string compName,string userName,string userPwd )
{
    try
    {
        DirectoryEntry entry = new DirectoryEntry("LDAP://test.com",userName,userPwd,AuthenticationTypes.Secure);
        DirectorySearcher mySearcher = new DirectorySearcher(entry);
       mySearcher.Filter = "(&(objectClass=computer)(|(cn=" + compName + ")(dn=" + compName + ")))";
       foreach (SearchResult result in mySearcher.FindAll())
       {
           if (result != null)
           {

               MessageBox.Show("computer GetDirectoryEntry():" + result.Path+"\n"+"computer path: "+result.Path);
                DirectoryEntry entryToRemove = new DirectoryEntry(result.Path,userName,userPwd);
                 entry.Children.Remove(entryToRemove);

               return true;
           }
           else
           {
               return false;
           }
       }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
    return false;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:03:53+00:00Added an answer on May 26, 2026 at 1:03 pm

    Using ADSI which is under System.DirectoryServices use a commit mechanism, here is a working sample :

    /* Retreiving RootDSE infos
     */
    string ldapBase = "LDAP://WM2008R2ENT:389/";
    string sFromWhere = ldapBase + "rootDSE";
    DirectoryEntry root = new DirectoryEntry(sFromWhere, "dom\\jpb", "PWD");
    string defaultNamingContext = root.Properties["defaultNamingContext"][0].ToString();
    
    /* Retreiving the computer to remove
     */
    sFromWhere = ldapBase + defaultNamingContext;
    DirectoryEntry deBase = new DirectoryEntry(sFromWhere, "dom\\jpb", ".biènèsph^r^.1966");
    
    DirectorySearcher dsLookForDomain = new DirectorySearcher(deBase);
    dsLookForDomain.Filter = "(&(cn=MACHSUPR))"; // MACHSUPR is the computer to delete
    dsLookForDomain.SearchScope = SearchScope.Subtree;
    dsLookForDomain.PropertiesToLoad.Add("cn");
    dsLookForDomain.PropertiesToLoad.Add("distinguishedName");
    
    SearchResultCollection srcComputer = dsLookForDomain.FindAll();
    
    foreach (SearchResult aComputer in srcComputer)
    {
      /* For each computer
       */
      DirectoryEntry computerToDel = aComputer.GetDirectoryEntry();
      computerToDel.DeleteTree();
      computerToDel.CommitChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any sample applications avaliable that make use of both caliburn and prism?
Is there any utility (or sample source code) that truncates HTML (for preview) in
I have a feeling that there must be client-server synchronization patterns out there. But
Is there any simple way to programatically colorize images in .NET? Basically we have
I want to create a simple application using Jena and SPARQL, that inserts, deletes,
I have two operations that are basically same: insert_bytes(from, count) delete_bytes(start, stop) -> delete_bytes(from,
I have a COM Interop DLL that I'm automatically generating from an existing COM
I have steps in the batch job that does different things. But before I
Is there any simple algorithm to determine the likeliness of 2 names representing the
Is there any simple way to generate a default crud (given an entity) with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.