The PowerShell Move-ADObject cmdlet moves any object or set of objects (such as a user, a computer, a group or an OU) to a specified OU. The -Identity parameter specifies which Active Directory object or container to move. Note that you need to enter the full LDAP path or SID of the object; you cannot use its SamAccountName.
Here’s how to move the user “John Brown” to the “Districts” OU:
Move-ADObject -Identity "CN=John Brown,CN=Users,DC=enterprise,DC=com" `
-TargetPath "OU=Districts,OU=IT,DC=Enterprise,DC=Com"
Use the same syntax to move computer objects. The following command will move the computer “R07GF” to the “Computers” container:
Move-ADObject -Identity "CN=R07GF,OU=CEO,DC=enterprise,DC=com" `
-TargetPath "CN=Computers,DC=Enterprise,DC=Com"
If you have a predefined list of objects to move, you can save it as a CSV file and then import that file to Active Directory. The CSV list should be in the following format:
Use this PowerShell script to move AD user accounts listed in a CSV file:
# Specify target OU. This is where users will be moved.
$TargetOU = "OU=Districts,OU=IT,DC=enterprise,DC=com"
# Specify CSV path. Import CSV file and assign it to a variable.
$Imported_csv = Import-Csv -Path "C:\temp\MoveList.csv"
$Imported_csv | ForEach-Object {
# Retrieve DN of user.
$UserDN = (Get-ADUser -Identity $_.Name).distinguishedName
# Move user to target OU.
Move-ADObject -Identity $UserDN -TargetPath $TargetOU
}
To move AD computer accounts listed in a text file, use the following PowerShell script:
# Specify path to the text file with the computer account names.
$computers = Get-Content C:\Temp\Computers.txt
# Specify the path to the OU where computers will be moved.
$TargetOU = "OU=Districts,OU=IT,DC=enterprise,DC=com"
ForEach( $computer in $computers) {
Get-ADComputer $computer |
Move-ADObject -TargetPath $TargetOU
}
