Link to home
Create AccountLog in
Avatar of nav2567
nav2567Flag for United States of America

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=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
Avatar of oBdA
oBdA

Well, then your "Name" column doesn't contain the SamAccountName of the user objects, except for the last one.
Since the sample csv you posted already contains the DN, you can use it directly; no need to retrieve a user object first.
Import-Module ActiveDirectory
# Specify target OU.
$TargetOU = "OU=TEST,DC=mydomain,DC=com"

Import-Csv -Path c:\test\PS\AD\moveusers\test.csv | ForEach-Object {
	# Move user to target OU.
	Move-ADObject -Identity $_.distinguishedName -TargetPath $TargetOU
}

Open in new window

Avatar of nav2567

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], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.MoveADObject
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer