In the post How to join a computer to domain it has been explained how to join a machine to a domain using Graphical User Interface(GUI). Some times we may have to do this from some script or programatically. We can use the tool Netdom.exe in such scenarios.
Below is the command we need to run for joining the machine to a domain.
netdom.exe join %computername% /domain:DomainName /UserD:DomainName\UserName /PasswordD:Password
The above command should be run from the machine which you want to join to the domain. The parameters which you need to replace are shown in bold letters.
Here Username and Password should be of a domain user having permissions to join a computer to the domain.
How to remove a computer from a domain using windows command line?
You can run the below command to remove a machine from the domain.
netdom.exe remove %computername% /domain:domainname /UserD:DomainName\UserName /PasswordD:Password
Netdom.exe can be downloaded from here