Join a Workstation to Active Directory with Shell and PowerShell

Published: July 13, 2023 | Last Modified: May 13, 2025

Tags: active-directory powershell shell windows automation

Categories: shell powershell



In this blog post, we will showcase concise and efficient commands that you can use in both Shell and PowerShell to join a workstation to Active Directory. These one-liner commands are practical for both quick use cases and scripting.

Shell

The following one-liner in Shell can join a workstation to Active Directory. It first invokes PowerShell to utilize its more sophisticated handling of secure credentials.

powershell -Command "& {$password = ConvertTo-SecureString 'thepassword' -AsPlainText -Force; $credential = New-Object System.Management.Automation.PSCredential ('domain\username', $password); Add-Computer -DomainName 'thedomainname' -Credential $credential -Restart -Force}"

In this command, replace ’thepassword’, ‘domain\username’, and ’thedomainname’ with the actual password, domain username, and domain name, respectively. After execution, the workstation will be forced to restart to apply the changes.

PowerShell

You can also use PowerShell directly to achieve the same result. The following command is similar to the Shell command but is run natively in PowerShell:

& {$password = ConvertTo-SecureString 'thepassword' -AsPlainText -Force; $credential = New-Object System.Management.Automation.PSCredential ('domain\username', $password); Add-Computer -DomainName 'thedomainname' -Credential $credential -Restart -Force}

Like with the Shell command, you need to replace ’thepassword’, ‘domain\username’, and ’thedomainname’ with the actual password, domain username, and domain name, respectively. The workstation will restart after this command is executed.