For this purpose, we can use PowerShell cmdlet Add-Computer. For example, below command:
Add-Computer -DomainName mydomain -Restart
will add the local computer to mydomain and restart the computer. It can also be used to switch the domain of the computer or join back to the workgroup. For example, below command can be used to join the local computer to a workgroup:
Add-Computer -WorkGroupName myworkgroup
We can also use this cmdlet to join domain and place the computer account in a particular OU. For example:
Add-Computer -DomainName mydomain -OUPath “OU=testOU,DC=mydomain,DC=com”
There are various switches available within this cmdlet:
–ComputerName : Specifies the computers to add to a domain or workgroup. The default is the local computer. Defaults to local computer, if not specified.
–Credential : Specifies a user account that has permission to join the computers to a new domain. Defaults to current user, if not specified.
–DomainName : Specifies the domain to which the computers are added.
–Force : Suppresses the user confirmation prompt. Without this parameter, Add-Computer requires you to confirm the addition of each computer.
–LocalCredential : Specifies a user account that has permission to connect to the computers that are specified by the ComputerName parameter. The default is the current user.
–NewName : Specifies a new name for the computer in the new domain. This parameter is valid only when one computer is being added or moved.
–OUPath : Specifies an organizational unit (OU) for the domain account. Enter the full distinguished name of the OU in quotation marks. The default value is the default OU for machine objects in the domain.
–PassThru : Returns the results of the command. By default, this cmdlet does not generate any output.
–Restart : Restarts the computers that were added to the domain or workgroup. A restart is often required to make the change effective.
–Server : Specifies the name of a domain controller that adds the computer to the domain. Enter the name in DomainName\ComputerName format. By default, no domain controller is specified.
–UnjoinDomainCredential : Specifies a user account that has permission to remove the computers from their current domains. The default is the current user.
–WorkgroupName : Specifies the name of a workgroup to which the computers are added. The default value is “WORKGROUP”. Cannot be used together with DomainName switch.
We can also pipe the computer names to this cmdlet. This can be helpful if you want to join computers to domain based on an certain inputs such as csv, excel files etc.