Automate joining a workstation to a domain using powershell and batch file

If you are a network admin looking for an easy way to script the joining of a workstation to a domain perhaps out of an image restore, you can do this easily by using powershell as well as a batch file to call your powershell script.

Your Powershell Script

Open up your favorite text editor and enter the following code, replacing the variables below with the appropriate information specific to your domain:

$credential = New-Object System.Management.Automation.PsCredential(“%useraccount%”, (ConvertTo-SecureString “%password%” -AsPlainText -Force))YourOUhere,DC=YOURDC,DC=YOURDC”)

Add-Computer -DomainName “%userdomain%” -Credential $credential -OUPath (“OU=YourOUhere,OU=

Name the script above as a .PS1 file as you will call this file with your batch file.

Your Batch File

The batch file code will contain commands to allow the execution policy to run the PS1 file you are calling and then set your policy back to restricted.  *Note* – the “joinDomain.ps1″ file will be the name of whatever you name your file you create above.
powershell Set-ExecutionPolicy Unrestricted

powershell c:deployjoinDomain.ps1

powershell Set-ExecutionPolicy Restricted

Popularity: 12% [?]

Related posts:

Installing Active Directory Certificate Services
Script to Add Users to Active Directory
Collecting User Info via Login Script

Share This Post

Recent Articles

  • Mark

    Hi:

    Thank you for this. Its something we have been looking to streamline our Windows 7 deployments. Unfortunately I’m a newbie at powershell so I’m in need of a little assistance.

    If my variables are the following:
    username: billybob
    password: secret
    CN=billybob,OU=Users,OU=Computer IT Admins,DC=ad,DC=mydomain,DC=com

    Computer I’m trying to join to the domain is called MYLABCOMP
    domain: ad.mydomain.com
    OU=Computers,OU=Computer IT Admins,DC=ad,DC=mydomain,DC=com

    I tried the following and it didn’t work

    $credential = New-Object System.Management.Automation.PsCredential(billybob, (ConvertTo-SecureString secret -AsPlainText -Force)) “OU=Users,OU=Computer IT Admins,DC=ad,DC=mydomain,DC=ca”)

    Add-Computer -DomainName ad.mydomain.com -Credential $credential -OUPath “OU=Computers,OU=Computer IT Admins,DC=ad,DC=mydomain,DC=com”

    Thanks.

    Mark

    • John Lee

      Mark,

      Are you receiving a specific error in Powershell? Can you send the output?

      Thanks,
      John

© 2012 Computer-Howto. All rights reserved. Site Admin · Entries RSS · Comments RSS