Automating Computer Organization in Active Directory with PowerShell

Managing a large fleet of computers within an Active Directory environment can be a daunting task, especially when it comes to organizing them into specific Organizational Units (OUs). In this blog post, we’ll explore how PowerShell scripting can be a powerful tool for automating the movement of computers based on specific conditions, streamlining administrative tasks and ensuring a well-organized Active Directory structure.

Consider a scenario where computers need to be moved to specific OUs based on their names. We have computers with names containing codes representing different cities – “hyd” for Hyderabad, “MUM” for Mumbai, “DEL” for Delhi, and “Kol” for Kolkata. Our goal is to automate the process of moving these computers to the corresponding OUs.

PowerShell Script: Below is a PowerShell script that achieves this task:

# Get all computers from Active Directory
$computers = Get-ADComputer -Filter *

# Loop through each computer
foreach ($computer in $computers) {
    $computerName = $computer.Name

    # Check conditions and move to the respective OU
    if ($computerName -like "*hyd*") {
        Move-ADObject -Identity $computer.DistinguishedName -TargetPath "OU=Hyderabad,DC=infoalias,DC=local" -Verbose
    } elseif ($computerName -like "*MUM*") {
        Move-ADObject -Identity $computer.DistinguishedName -TargetPath "OU=Mumbai,DC=infoalias,DC=local" -Verbose
    } elseif ($computerName -like "*DEL*") {
        Move-ADObject -Identity $computer.DistinguishedName -TargetPath "OU=Delhi,DC=infoalias,DC=local" -Verbose
    } elseif ($computerName -like "*Kol*") {
        Move-ADObject -Identity $computer.DistinguishedName -TargetPath "OU=Kolkata,DC=infoalias,DC=local" -Verbose
    }
}

Write-Host "Computer move script completed."

This script:

  • The script retrieves all computer objects from Active Directory using the Get-ADComputer cmdlet.
  • It then iterates through each computer, extracting its name.
  • Based on specific conditions (presence of “hyd,” “MUM,” “DEL,” or “Kol” in the name), the script uses the Move-ADObject cmdlet to move the computer to the appropriate OU.

Preparation: Before running the script, ensure:

  1. The Active Directory module is available.
  2. You have the necessary permissions to move objects in Active Directory.
  3. Adjust the script to match your domain information.
  4. Confirm that the target OUs (OU=Hyderabad, OU=Mumbai, OU=Delhi, OU=Kolkata) already exist in Active Directory.

Automating routine tasks like organizing computers in Active Directory can save time and reduce the risk of errors. PowerShell provides a flexible and powerful scripting environment for such tasks, allowing administrators to focus on more strategic aspects of IT management. By leveraging scripts tailored to specific conditions, administrators can maintain a well-organized and efficient Active Directory structure.

In conclusion, this script is a starting point for automating computer organization based on specific criteria. Feel free to customize and expand it to suit your organization’s unique requirements and further enhance your Active Directory management capabilities.