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:

  1. Administrative Rights: You need elevated permissions to add computers to AD.
  2. 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

  1. Module Import:
    The script imports the Active Directory module to access AD commands.
  2. CSV Import and Validation:
    It reads the CSV file, and if the file is empty, it terminates the process with a warning.
  3. 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-ADComputer command.
    • If an error occurs, the script catches it and displays a relevant message.
  4. Error Handling:
    The script uses a try-catch block 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.

By amit_g

Welcome to my IT Infra Blog! My name is Amit Kumar, and I am an IT infrastructure expert with over 11 years of experience in the field. Throughout my career, I have worked with a wide variety of systems and technologies, from network infrastructure and cloud computing to hardware and software development. On this blog, I aim to share my knowledge, insights, and opinions on all things related to IT infrastructure. From industry trends and best practices to tips and tricks for managing complex systems, my goal is to provide valuable information that will help IT professionals and enthusiasts alike. Whether you are a seasoned IT veteran or just getting started in the field, I hope you will find my blog to be a valuable resource. In addition to sharing my own thoughts and ideas, I also welcome feedback, comments, and questions from my readers. I believe that a collaborative approach is the best way to advance the field of IT infrastructure and I look forward to hearing from you. Thank you for visiting my blog, and I hope you will continue to follow along as I explore the fascinating world of IT infrastructure. Sincerely, Amit Kumar

Leave a Reply

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