April 4, 2016
Move Bulk AD user from One OU to another using Powershell.
As of the transfer with in the organization of different departments or branch happens, it is hetic job for the IT Admin to manage all the users from one OU to another. Doing it manually really kills the time and makes insane. So here, i have create a script that could help IT admin to get relief of such hetic job. In this script I made a source as a CSV file where IT Admin can just have a user CN and Target OU and boom every users will get transfer to the desired OU.
################################################################# # This script will help yo move bulk ad accounts into target OU # Written 04/04/2016 Prashant,Dhewaju # Fell free to change use any part of this script # http://pdhewaju.com.np ################################################################# # Import AD Module import-module ActiveDirectory # Import CSV # Import the data from CSV file and assign it to variable $Imported = Import-Csv -Path "C:\temp\move.csv" $Imported | ForEach-Object { # Retrieve DN of User. $UserDN = (Get-ADUser -Identity $_.Username).distinguishedName $TargetOU = $_.TargetOU Write-Host " Moving Accounts ..... " # Move user to target OU. Move-ADObject -Identity $UserDN -TargetPath $TargetOU } Write-Host " Completed move " $total = ($Imported).count Write-Host $total "User Moved Successfully"
Download script and sample CSV format from this link
20 Comments
Dear Sirs,
I have got the problems when run this scrip, below is error. Could you please give me an advice?
User Moved Successfully
PS C:scriptsmove> .moveUser.ps1
Moving Accounts …..
Move-ADObject : Access is denied
At C:scriptsmovemoveUser.ps1:20 char:19
+ Move-ADObject <<<< -Identity $UserDN -TargetPath $TargetOU
+ CategoryInfo : PermissionDenied: (CN=Thuan Phan,O…svietnam,DC=com:ADObject) [Move-ADObject], Unauthor
izedAccessException
+ FullyQualifiedErrorId : Access is denied,Microsoft.ActiveDirectory.Management.Commands.MoveADObject
Completed move
User Moved Successfully
Many thanks.
Best regards,
Thuan
Hi Thuan,
Can you please use your Admin Powershell mode. It should be working with it. seems like you didn’t have enough credential for this purpose.
Dear Admin,
Thanks so much for your advice.
I did as your advice already but I have got another problems, please see below message.
——————————
PS C:scriptsmove> .moveUser.ps1
Moving Accounts …..
Move-ADObject : The operation could not be performed because the object’s parent is either uninstantiated or deleted
At C:scriptsmovemoveUser.ps1:20 char:19
+ Move-ADObject <<<< -Identity $UserDN -TargetPath $_.TargetOU
+ CategoryInfo : NotSpecified: (CN=test,OU=IT,O…svietnam,DC=com:ADObject) [Move-ADObject], ADException
+ FullyQualifiedErrorId : The operation could not be performed because the object's parent is either uninstantiate
d or deleted,Microsoft.ActiveDirectory.Management.Commands.MoveADObject
————————————————
Could you please give me one more advice?
Thank you,
Thuan
Dear Admin,
I am wrong the OU=com with DC=com.
Thank you so much for your advice.
Best regards,
Thuan
Hi,
I followed the instructions and copied the style of your example csv but I can’t seem to get this to work. The powershell window just closes, no error messages or anything.
Hi Mohammed,
can you please help me with you result, what actually happened when you tried to run this script?
Nothing, I right click and hit open with powershell, the powershell window opens for like a second or so then just closes.
Hi Mohammed, you need to make some edit on the script as per your environment, like the location of CSV file and detail of your domain controller.
Also Mohammed,
it will be great if you could download every thing related this script from TechNet Gallery below is the link or it’s itself on the blog to download the CSV file too.
https://gallery.technet.microsoft.com/Move-Bulk-AD-user-from-One-1d4cf28c
Having a problem moving mine.. need some help please..
# Import CSV
# Import the data from CSV file and assign it to variable
$Imported = Get-Content -Path “C:\temp\move.csv”
$Imported | ForEach-Object {
# Retrieve DN of User.
$UserDN = (Get-ADUser -Identity $_.Username).distinguishedName
$TargetOU = $_.TargetOU
Write-Host ” Moving Accounts ….. ”
# Move user to target OU.
Move-ADObject -Identity $UserDN -TargetPath $TargetOU “OU=Temp Disabled Users,OU=duckerEnergy.com,DC=tesg,DC=duckerenergy,DC=com”
}
Write-Host ” Completed move ”
$total = ($Imported).count
Write-Host $total “User Moved Successfully”
…
I think i am putting my target OU info in the wrong place. can someone please advise…
Hi Demitri,
can you help me with your domain name and path of the OU on which you want to move the users… although can you try this format?
— Move-ADObject -Identity $UserDN -TargetPath $TargetOU “OU=Temp Disabled Users,DC=duckerenergy,DC=com” —
appreciate your feedback on this ?
Hi Demitri,
What if the destination OU has the same name as the user you’re trying to move into that OU. How would you add the duplicate name to the same OU?
You cannot have unique login name in AD, i.e. unique samaccount name. So you don’t need to worry with display name.
Hi Pdhewjau,
Thank you for the helpful instructions.
I attempted to move user one OU to another without csv.
Unfortunately, some reason i get : Get-ADObject : A positional parameter cannot be found that accepts argument
Not sure if i do the things right way. Could you please look into it and let me know where i can fix.
Thank you.
Import-Module ActiveDirectory
$Imported = Get-ADObject -Filter *SearchBase “OU=Hamal_new,DC=dircolab,DC=com”
$Imported |ForEach-Object {
$UserDN = (Get-ADUser Identity $_.Username).distinguishedName
$targetOU = “OU=Managers,DC=dircolab,DC=com”
Write-Host “Moving Accounts …”
Move-ADObject -Identity $UserDN -TargetPath $targetOU
}
Write-Host “Completed move”
$Total = ($Imported).count
Write-Host $Total “User Moved Successfully”
Hi Dirco,
Which version of Active Directory you are working with and can you check if you can do Import-Module Active Directory.
hi,
ı experienced this error today and solved it by correcting ou path.
thanks…
You’re the best
Good Day
I am getting this error.
PS C:\Windows\system32> C:\AD Scripts\Bulk user Move\Bulk user move _v1.0.ps1
Get-ADUser : Cannot find an object with identity: ‘*********,”OU=PLP,OU=NCV Students,OU=Active Students,DC=******,DC=local”‘ under:
‘DC=*******,DC=local’.
At C:\AD Scripts\Bulk user Move\Bulk user move _v1.0.ps1:16 char:18
+ $UserDN = (Get-ADUser -Identity $_.Username).distinguishedName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (161739482,”OU=P…bstud,DC=local”:ADUser) [Get-ADUser], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Managem
ent.Commands.GetADUser
Moving Accounts …..
Move-ADObject : Cannot validate argument on parameter ‘Identity’. The argument is null or an element of the argument collection contains a null value.
At C:\AD Scripts\Bulk user Move\Bulk user move _v1.0.ps1:20 char:31
+ Move-ADObject -Identity $UserDN -TargetPath $TargetOU
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Move-ADObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.MoveADObject
Completed move
User Moved Successfully
PS C:\Windows\system32>
Hello admin
The OU on the device need to be rfdbeaufort.local/_Survitec Services/_Survitec Services US/Service Miami/Computers/
Can you advise how to add the line to the script ?
Thank you
Hi Claudiu,
you need to manage that with Excel sheet. There is link to download Sample CSV at last of my blog.