Powershell to Add Computers to AD

When I work with clients to assist with rapid building computers, often I will use the Wizard, but there are situations where I prefer to stage the computers in AD first. Rarely, do I want to build a group of computers one by one. So I use powershell to do this. Make sure you import the Active Directory module before you start. You may have to download the RSAT tools from Microsoft and enable them on your system. This script uses all of the native tools from Microsoft.

import-module activedirectory

The following script works, but you have to put all of the computers in a text file. (note: this scriptlet relies on the Quest AD modules)

Get-Content "C:\file.txt" | ForEach-Object {Get-QADComputer $_ |`
Add-QADGroupMember -Credential $usercredential -Identity "ad_group" }

With a little creativity I am able to set the prefix and a counter and build the computers on the fly without having to put everything in a text file and read it. And I am using the RSAT tools from Microsoft and not third-party tools.

$start = 100
$counter = 0
$prefix = "BASE"
$ComputerOU = [ADSI]“LDAP://OU=Workstations,DC=contoso,DC=com“
do {
# Create the computer name
$suffix = $start + $counter
$ComputerName = $prefix + $suffix
# Assign the Variables
$samname = $ComputerName + "$"
$Computer = $ComputerOU.create(“Computer”,”cn=$ComputerName”)
# Commit the changes
write-host "Adding $ComputerName to target OU"
while ($counter -lt $num)

This will create ten computers BASE100 to BASE109 and add them to the OU listed above. I should add some code to capture errors, and code to capture what happens and output that to a text file. But you get the idea. You can also make the names as complex or as simple as you wish. With this, I don't have to take the time to create a text file, I just set the parameters and run, thus eliminating an "unnecessary" step.