Joining a Windows 7 Client to a Domain through PowerShell

Time to join a Windows 7 client to a domain. Now that the Test Domain is up and running ( Check Previous Posts ), we can start joining clients to the domain.

Since it’s a freshly installed Windows 7 client, we dont have to bother about userprofiles and so on.. we’ll save that for later :)

Joining the domain is done through WMI. Just create a new Object containing the Win32_ComputerSystem WMI object and call the JoinDOmainOrWorkGroup() method.


function JoinDomain ([string]$Domain, [string]$User, [string]$Password ) {

	$DomainUser = $Domain + "\" + $User
	$OU = $null

	$ComputerSystem = gwmi Win32_ComputerSystem

	$ComputerSystem.JoinDomainOrWorkGroup(
		$Domain, 
		$Password, 
		$DomainUser, 
		$OU, 
		3
	)
}

Here’s an example on running the function.


PS > JoinDomain PowerShell.nu migaccount Password1

And voila! the client shuts down and on Startup, It’ll have joined the new domain.

Rating 3.00 out of 5
[?]

2 thoughts on “Joining a Windows 7 Client to a Domain through PowerShell

  1. Windows 7 (PowerShell V2) comes with the Add-Computer cmdlet that adds the local computer to a domain or workgroup, or moves it from one domain to another.

    All we need to do to add a computer to domain Domain01:

    C:\PS>add-computer -domainname Domain01 -cred Domain01\Admin01
    C:\PS>restart-computer

  2. Is there a way to have this method rename the computer and join the domain at the same time??

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Anti-Spam Protection by WP-SpamFree