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 8589179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:59:19+00:00 2026-06-11T22:59:19+00:00

I’ve been trying to write a quick and dirty C# .exe that I can

  • 0

I’ve been trying to write a quick and dirty C# .exe that I can distribute to some student workers in our IT office. The .exe should be able to detect the name of the machine on which it’s being run, search for that name in Active Directory, and disable the computer entry. So far I haven’t had a problem with the name detection or search, but the bit of removal code is giving me a false positive when I can go directly into Active Directory to see that the computer entry has not been disabled.

    private void confirmRemoveButton_Click(object sender, EventArgs e)
    {
        string computerName = Environment.MachineName;
        using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, null, "useraccount", "password"))
        {
            ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(domainContext, computerName);

            if (computer != null)
            {
                try
                {
                    computer.Enabled = false;
                    label3.Visible = true;
                    label3.Text = "Computer was disabled in Active Directory.";
                    button1.Visible = true;
                }

                catch (Exception x)
                {
                    label3.Visible = true;
                    label3.Text = "Unable to disable computer with exception " + x;
                    button1.Visible = true;
                }
            }

            else if (computer == null)
            {
                label3.Visible = true;
                label3.Text = "Computer was not found in Active Directory.";
                button1.Visible = true;
            }

            else
            {
                label3.Visible = true;
                label3.Text = "Unexpected error in computer search.";
                button1.Visible = true;
            }
        }
    }

This is the code I have right now; the preceding code is about having the user check the computer name against the detected computer name and confirm that they actually want to disable the computer account. Once they click to confirm this (misleadingly currently labeled as confirm removal button), it should run this code to report success or failure. However, in testing, it reports success though I can see the computer object is not disabled.

This link (http://stackoverflow.com/questions/591681/using-c-how-do-you-check-if-a-computer-account-is-disabled-in-active-directory) is a topic relating to disabling a computer account in the title, but the comments and code all seem to suggest that this applies to disabling a user account.

Any insight would be appreciated 🙂

  • 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-06-11T22:59:20+00:00Added an answer on June 11, 2026 at 10:59 pm

    You have to save the PrincipalComputer object. Otherwise your code is fine. Here’s a simple console app version which will return nothing if the computer doesn’t exist.

        static void Main(string[] args)
        {
            Console.WriteLine("Enter the name of the computer you wish to disable");
            string ComputerName = Console.ReadLine();
            if (ComputerName != "" && ComputerName != null)
            {
                using (PrincipalContext TargetDomain = new PrincipalContext(ContextType.Domain, null, "admin", "password"))
                {
                    ComputerPrincipal TargetComputer = ComputerPrincipal.FindByIdentity(TargetDomain, ComputerName);
    
                    if (TargetComputer != null)
                    {
                        if ((bool)TargetComputer.Enabled)
                        {
                            Console.WriteLine("Computer is currently enabled, it will now be disabled");
                            TargetComputer.Enabled = false;
                            Console.WriteLine("Is computer now enabled? " + TargetComputer.Enabled);
                            TargetComputer.Save();
                        }
                        else
                        {
                            Console.WriteLine("Computer is currently disabled, it will now be enabled");
                            TargetComputer.Enabled = true;
                            Console.WriteLine("Is computer now enabled? " + TargetComputer.Enabled);
                            TargetComputer.Save();
                        }
                        Console.Read();
                    }
                }
            }
        }
    

    dang, Kieren beat me to it!

    Note, sometimes it can take awhile before AD recognizes what has happened.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.