nav2567
asked on
Bulk move AD users to another OU
Hello,
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=co m"
# Read user sAMAccountNames from csv file (field labeled "Name").
Import-Csv -Path c:\test\PS\AD\moveusers\te st.csv | ForEach-Object {
# Retrieve DN of User.
$UserDN = (Get-ADUser -Identity $_.Name).distinguishedName
# Move user to target OU.
Move-ADObject -Identity $UserDN -TargetPath $TargetOU
}
TEST.CSV
distinguishedName
CN=Carl Rivet,OU=Users,OU=YUL,OU=_ CANADA,DC= mydomain,D C=com
CN=BA Sales,OU=Users,OU=IAD5,OU= IAD_,OU=_U S,DC=mydom ain,DC=com
CN=Vida Poon,OU=Users,OU=IT,OU=COR P_,OU=_US, DC=mydomai n,DC=com
CN=HYSynergy,OU=Service Accounts,OU=IT,OU=CORP_,OU =_US,DC=my domain,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], ADIdentityNotFoundExceptio n
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Micr osoft.Acti veDirector y.Manageme nt.ADIdent ityNotFoun dException ,Microsoft .ActiveDir ectory.Man agement.Co mmands.Get ADUser
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], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.MoveADO bject
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=co
# Read user sAMAccountNames from csv file (field labeled "Name").
Import-Csv -Path c:\test\PS\AD\moveusers\te
# Retrieve DN of User.
$UserDN = (Get-ADUser -Identity $_.Name).distinguishedName
# Move user to target OU.
Move-ADObject -Identity $UserDN -TargetPath $TargetOU
}
TEST.CSV
distinguishedName
CN=Carl Rivet,OU=Users,OU=YUL,OU=_
CN=BA Sales,OU=Users,OU=IAD5,OU=
CN=Vida Poon,OU=Users,OU=IT,OU=COR
CN=HYSynergy,OU=Service Accounts,OU=IT,OU=CORP_,OU
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], ADIdentityNotFoundExceptio
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Micr
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], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
ASKER
I am getting an error similar to the below
Move-ADObject : Cannot find an object with identity: 'mrivet' under: 'DC=mydomain,DC=com'.
At line:7 char:2
+ Move-ADObject -Identity $_.samAccountName -TargetPath $TargetOU
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (mrivet:ADObject) [Move-ADObject], ADIdentityNotFoundExceptio n
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Micr osoft.Acti veDirector y.Manageme nt.ADIdent ityNotFoun dException ,Microsoft .ActiveDir ectory.Man agement.Co mmands.Mov eADObject
Move-ADObject : Cannot find an object with identity: 'mrivet' under: 'DC=mydomain,DC=com'.
At line:7 char:2
+ Move-ADObject -Identity $_.samAccountName -TargetPath $TargetOU
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (mrivet:ADObject) [Move-ADObject], ADIdentityNotFoundExceptio
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Micr
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Since the sample csv you posted already contains the DN, you can use it directly; no need to retrieve a user object first.
Open in new window