← script library

Move AD Users to an OU by Department with PowerShell

Active DirectoryAugust 17, 2026

Reads every user out of a staging OU — the one an HR sync or provisioning tool writes into — and moves each into the target OU mapped to its department attribute. Users with no department, or a department with no mapping, are reported and left alone. The mapping is a hashtable at the top of the script.

powershell
# Active Directory: Move AD Users based on Department ex. QUO-SYNC
#-------------------------------------------

Import-Module ActiveDirectory

# Domain
$DomainDN = (Get-ADDomain).DistinguishedName

# Source OU (Q.U.O. Sync folder)
$SourceOU = "OU=QUO-SYNC,$DomainDN"

# Department → Target OU mapping
$DepartmentOUs = @{
    "IT"        = "OU=IT,OU=Users,OU=Company,$DomainDN"
    "HR"        = "OU=HR,OU=Users,OU=Company,$DomainDN"
    "Finance"   = "OU=Finance,OU=Users,OU=Company,$DomainDN"
    "Marketing" = "OU=Marketing,OU=Users,OU=Company,$DomainDN"
}

# Move each user to respective OU
Get-ADUser -SearchBase $SourceOU -Filter * -Properties Department |
ForEach-Object {

    $User = $_
    if (-not $User.Department) {
        Write-Error "Skipping $($User.SamAccountName): No Department set"
    }
    if (-not $DepartmentOUs.ContainsKey($User.Department)) {
        Write-Error "Skipping $($User.SamAccountName): No OU mapped for department '$($User.Department)'"
    }
    $TargetOU = $DepartmentOUs[$User.Department]

    try {
        Move-ADObject -Identity $User.DistinguishedName -TargetPath $TargetOU
        Write-Host "Moved $($User.SamAccountName) to $TargetOU"
    }
    catch {
        Write-Error "Failed to move $($User.SamAccountName): $_"
    }
}

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.