Managing computers in a corporate environment can be challenging, especially when you need to add multiple machines to Active Directory (AD). Doing this task manually is time-consuming, but PowerShell provides a quick and efficient way to automate the creation of bulk computers. This blog will walk you through the steps of using PowerShell to create multiple computer accounts in AD using a CSV file.
Prerequisites
Before diving into the PowerShell script, ensure you have the following:
- Administrative Rights: You need elevated permissions to add computers to AD.
- Active Directory Module for PowerShell: This can be installed using powershell with below command
Install-WindowsFeature RSAT-AD-PowerShell
3. CSV File: Prepare a CSV file with the list of computers you wish to add.
ComputerName
PC01
PC02
PC03
PowerShell Script to Create Computers
Here’s a simple script that reads the computer names from the CSV file and creates each computer account in a specific Organizational Unit (OU) within AD.
# Import Active Directory module
Import-Module ActiveDirectory
# Define the path to the CSV and the target OU
$csvPath = "C:\Path\To\computers.csv"
# Here we are using default computers location with Computers You can chagne OU path if you are using custom OU instead of Computers directory
$targetOU = "CN=Computers,DC=example,DC=com"  # Replace with your OU path
# Import the CSV file
$computers = Import-Csv -Path $csvPath
# Validate if the CSV contains data
if ($computers.Count -eq 0) {
    Write-Host "CSV file is empty. Please verify the content." -ForegroundColor Yellow
    exit
}
# Loop through each computer entry in the CSV
foreach ($computer in $computers) {
    $ComputerName = $computer.ComputerName.Trim()
    if ([string]::IsNullOrEmpty($ComputerName)) {
        Write-Host "Skipping empty computer name entry." -ForegroundColor Yellow
        continue
    }
    try {
        # Create computer account in the specified OU
        New-ADComputer -Name $ComputerName -Path $targetOU -Enabled $true -SamAccountName $ComputerName
        Write-Host "Successfully created computer: $ComputerName" -ForegroundColor Green
    }
    catch {
        Write-Host "Failed to create computer: $ComputerName. Error: $_" -ForegroundColor Red
    }
}
Explanation of the Script
- Module Import:
 The script imports the Active Directory module to access AD commands.
- CSV Import and Validation:
 It reads the CSV file, and if the file is empty, it terminates the process with a warning.
- Computer Creation Loop:
 For each computer name in the CSV:- It trims whitespace from the name to avoid formatting issues.
- If the name is valid, it adds the computer to the specified OU using the New-ADComputercommand.
- If an error occurs, the script catches it and displays a relevant message.
 
- Error Handling:
 The script uses atry-catchblock to handle errors gracefully.
Conclusion
Using PowerShell to create bulk computer accounts in Active Directory saves time, reduces errors, and ensures consistency. With the help of a simple CSV file and the New-ADComputer command, administrators can automate the process seamlessly. This script is scalable and can be reused whenever you need to add new machines.