Working with the Add-Computer Cmdlet in PowerShell
Welcome back to Wahmans Powershell blog! Today, we are going to look at a very handy cmdlet for system administrators: Add-Computer. This cmdlet is used to add the local computer to a domain or a workgroup, making it an essential part of many enterprise automation tasks.
According to Microsoft Docs:
Add-Computer
– Adds the local computer to a domain or workgroup.
Let’s dig into some practical use cases for Add-Computer
, ranging from beginner to advanced.
Example 1: Add a Local Computer to a Workgroup (Beginner)
If you want to move your computer out of a domain and into a workgroup, you can use the following command:
Add-Computer -WorkGroupName "MyWorkGroup" -Restart
This command switches your computer to a workgroup named MyWorkGroup
and restarts it to apply the change.
Example 2: Join a Domain with Prompted Credentials (Intermediate)
To join your machine to a domain, you usually need domain credentials. This script will prompt for them:
Add-Computer -DomainName "contoso.com" -Credential (Get-Credential) -Restart
This command lets the user input their username and password using a dialog box, then joins the domain and restarts the machine.
Example 3: Join a Domain with a Specific OU Path (Advanced)
Sometime you want the computer account to land in a specific OU (Organizational Unit). You can specify it like this:
$cred = Get-Credential
Add-Computer -DomainName "contoso.com" -Credential $cred -OUPath "OU=Workstations,DC=contoso,DC=com" -Restart
This command uses a specific OU path so the computer account goes exactly where you want it in Active Directory.
Example 4: Add Multiple Computers to a Domain Remotely (Power User)
If you’re managing multiple machines, you can use PowerShell Remoting to join them all to a domain:
$computers = @("PC1", "PC2", "PC3")
$cred = Get-Credential
foreach ($computer in $computers) {
Invoke-Command -ComputerName $computer -ScriptBlock {
Add-Computer -DomainName "contoso.com" -Credential $using:cred -Restart
} -Authentication Default
}
This command joins several remote computers to the domain using remoting and then restarts them.
That’s all for today’s deep dive into Add-Computer
! It’s a powerful cmdlet that can make domain joins and workgroup configurations a breeze when used wisely! Whether you’re just starting or building advanced deployment scripts, now you’ve got a few more tools in your toolbox.
Happy scripting, and I will see you in the next post!
Leave a Reply