troubleshooting Question

Bulk move AD users to another OU

Avatar of nav2567
nav2567Flag for United States of America asked on
Active DirectoryPowershell
3 Comments1 Solution131 ViewsLast Modified:
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=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
}




TEST.CSV

distinguishedName
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros