Move Ad User to another OU with PowerShell

Move-AdObject cmdlet in PowerShell is used to move ad users to another OU. Use Get-AdUser cmdlet to get active directory user and pipe aduser object to Move-AdObject.

Move-AdObject cmdlet moves an object or container of objects from one location to another. In this blog post, we will learn how to use the Move-AdObject cmdlet to move ad users to another OU.

Move Ad User to Another OU

Using Move-AdObject to move ad user to another OU. Use the below command

Get-ADUser -Identity Tira.Elsa | Move-ADObject -TargetPath "OU=HR,DC=SHELLPRO,DC=LOCAL"

In the above PowerShell script,

Tira.Elsa active directory user belongs to SHELL Users OU. The first command Get-AdUser gets an active directory user using SamAccountName and passes the output to the second command.

The second command uses Move-AdObject to move ad user object to another OU specified by the TargetPath parameter.

The above command moves ad user Tira.Elsa to HR OU. The output of the above script is given below

PowerShell Move Ad User
PowerShell Move Ad User

Cool Tip: How to Disable active directory user using PowerShell!

Move Ad User to OU using CSV

You can bulk move ad users to OU using a CSV file. CSV file contains a list of active directory users SAMAccountName.

Using Move-AdObject cmdlet in Powershell to move ad users to different OU using CSV file as given below

$adUsers = Import-Csv -Path "C:\PowerShell\AdusersList.csv"
$TargetOU = "OU=HR,DC=SHELLPRO,DC=LOCAL"
$adUsers | ForEach-Object {

    # Get ad user
    $aduser = (Get-ADUser -Identity $_.SAMAccountName).distinguishedName

    Write-Host "Move ad user" $aduser

    # Move user to target OU
    Move-ADObject -Identity $aduser -TargetPath $TargetOU 
} 

In the above PowerShell script,

Using Import-CSV cmdlet, it import a CSV file and store information in $adUsers variable.

$TargetOU variable contains different OU path where we need to move ad user using Move-AdObject.

Using For-Each loop, it iterates over $adUsers objects. Using the Get-AdUser cmdlet, it gets an active directory user object using SAMAccountName and stores an ad user object in the $aduser variable.

In the next command, the Move-AdObject cmdlet moves ad user to different OU specified by the TargetPath parameter.

Conclusion

In the above, we learned how to use the Move-AdObject cmdlet in PowerShell to move ad user object to another OU.

To bulk move ad users to OU from CSV, import the list of ad users from CSV and pass ad user object to Move-AdObject to move ad user.

AD Benefits: Read more to know Active Directory advantages and disadvantages!

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment