There are many ways how to join a Windows machine to a domain. You can use for example command line or PowerShell script. I will show you how to do the same thing using a compiled EXE file written in C#.

No special preparation is required. Open your Visual Studio, create a new console application and paste the code below. You will need to add a reference to System.Management library.

using System;
using System.Management;

namespace DomainJoin
    class Program
        static void Main(string[] args)
            string domain = "";
            string password = "secret";
            string username = "EXAMPLE\\administrator";
            string OU = "OU=Computers,DC=example,DC=com";

            ManagementObject managementObject = new ManagementObject("Win32_ComputerSystem.Name=\"" + Environment.MachineName + "\"");
            managementObject.Scope.Options.Authentication = AuthenticationLevel.PacketPrivacy;
            managementObject.Scope.Options.Impersonation = ImpersonationLevel.Impersonate;
            managementObject.Scope.Options.EnablePrivileges = true;
            int mode = 1 | 2;
            string[] parameters = new string[5]
            int returnCode = Convert.ToInt32(managementObject.InvokeMethod("JoinDomainOrWorkgroup", parameters));
            Console.WriteLine("Domain join has completed with code " + returnCode);

Build the application and give it a shot. Keep an eye on the return code, that will be printed to console. If it’s zero you made it and the machine is successfully in the domain. If not refer to official documentation in order to check what went wrong.

Same documentation also describes all the different modes you can use. In my case, I used 1 and 2, which means “join the machine to domain” and “create a new account for that machine”. You may need to add more based on your use case.