Home > Projects, Windows 7 > Joining a Windows 7 Client to a Domain through PowerShell

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
[?]
Categories: Projects, Windows 7 Tags: ,
  1. June 6th, 2009 at 21:46 | #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. Christian
    March 25th, 2010 at 15:37 | #2

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

  1. No trackbacks yet.

Spam Protection by WP-SpamFree