Avatar of nav2567
Flag for United States of America

asked on 

Bulk move AD users to another OU


I am trying the below script to read AD users in the TEST.CSV file and move them to OU=TEST,DC=mydomain,DC=com.

Import-module activedirectory
# Specify target OU.
$TargetOU = "OU=TEST,DC=mydomain,DC=com"

# Read user sAMAccountNames from csv file (field labeled "Name").
Import-Csv -Path c:\test\PS\AD\moveusers\test.csv | ForEach-Object {
    # Retrieve DN of User.
    $UserDN = (Get-ADUser -Identity $_.Name).distinguishedName

    # Move user to target OU.
    Move-ADObject -Identity $UserDN -TargetPath $TargetOU


CN=Carl Rivet,OU=Users,OU=YUL,OU=_CANADA,DC=mydomain,DC=com
CN=BA Sales,OU=Users,OU=IAD5,OU=IAD_,OU=_US,DC=mydomain,DC=com
CN=Vida Poon,OU=Users,OU=IT,OU=CORP_,OU=_US,DC=mydomain,DC=com
CN=HYSynergy,OU=Service Accounts,OU=IT,OU=CORP_,OU=_US,DC=mydomain,DC=com

After the script is run, only the last user in the TEST.CSV file is moved (HYSYNERGY).  Other users shows error like this:


Get-ADUser : Cannot find an object with identity: 'Marc Rivet' under: 'DC=mydomain,DC=com'.
At line:8 char:16
+     $UserDN = (Get-ADUser -Identity $_.Name).distinguishedName
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Carl Rivet:ADUser) [Get-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Move-ADObject : Cannot validate argument on parameter 'Identity'. The argument is null or an element of the argument collection contains a null value.
At line:11 char:29
+     Move-ADObject -Identity $UserDN -TargetPath $TargetOU
+                             ~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Move-ADObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.MoveADObject
PowershellActive Directory

Avatar of undefined
Last Comment

8/22/2022 - Mon