Powershell script to move AD Computer accounts to a new OU Container

Hey,

I've generated a list AD user computer accounts to a csv. Most of these PC accounts are spread among 10+ OU containers,

What I now need to do is get a PowerShell script to run against AD, which will move each of the computer accounts in the csv and move it to a new OU container I created.

I've come to conclusion that I suck at PowerShell so hopefully one of our fantastic community members might be able to help me out

FYI, if it matters, its a Server 2008R2 DC

Thanks in advance
QuazzieMAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Peter HutchisonSenior Network Systems SpecialistCommented:
Maybe something like this would work. CSV contains two columns: Name and OU.
e.g.
COMPUTER1, OU=Sales,DC=mycompany,DC=com
COMPUTER2, OU=Production,DC=mycompany,DC=com


Param($filename)
Import-Module ActiveDirectory
$complist = Import-CSV -Path $filename
foreach ($obj in $complist)
{
 Write-Output "Moving $obj.Name to  $obj.OU"
  Move-ADObject -Identity $obj.Name -TargetPath $obj.OU
}
0
Manikandan NarayanswamySecurity Specialist & IBM Security GuardiumCommented:
Hi,

This is the simplest way of moving computer accounts to OU

Get-ADComputer -Filter * -Property * | Select-Object Name,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion | Export-CSV AllWindows.csv -NoTypeInformation -Encoding UTF8

Once it's exported to csv use the following script

$ComputersPath= Import-Csv -Path "D:\Files\Computers.csv"
$TargetOU = "OU=NewOU,DC=myDomain,DC=local"
foreach ($item in $ComputersPath){

    $computer = Get-ADComputer $item.CompName

    Move-ADObject -Identity $computer.DistinguishedName -TargetPath $TargetOU -Confirm:$false

    Write-Host Computer account $computer.Name has been moved successfully
      
   
            }

Thanks
Manikandan
0
Guy LidbetterCommented:
@Manikandan... You really, really don't need all that info... just the Name and OU...

I assume you have the PC name, and the new destination OU for all the accounts which is all you really need.

So, if the CSV is only a "Name" column

$Computers= Import-Csv CSVFile.csv
$TargetOU = "OU=NewOU,DC=Domain,DC=com"

foreach ($Computer in $Computers) {
    Move-ADObject -Identity $computer.Name -TargetPath $TargetOU -Confirm:$false
    Write-Host $computer.Name has been moved successfully -ForegroundColor Green
}

Open in new window


Regards

Guy
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

QuazzieMAuthor Commented:
Hey Guys,
I've attached the and csv file and the error I got when I ran it.

I created the PC.ps1 file with the following info in it

$Computers= Import-Csv C:\PC.csv
$TargetOU = "OU=TEST,OU=DEV_Computers,DC=dev,DC=local"

foreach ($Computer in $Computers) {
    Move-ADObject -Identity $computer.Name -TargetPath $TargetOU -Confirm:$false
    Write-Host $computer.Name has been moved successfully -ForegroundColor Green
}

Open in new window


It seems to be having an issue with the 'Identity' part of the script, can you please review it see what could be the issue.
PC.csv
PS-error.JPG
0
Manikandan NarayanswamySecurity Specialist & IBM Security GuardiumCommented:
Hi,

Use the below command to first export the computer name to CSV file properly. Then use the script to import it and move it to the new OU

Get-ADComputer -Filter * -Property * | Select-Object Name | Export-CSV AllWindows.csv -NoTypeInformation -Encoding UTF8

Thanks
Manikandan
0
Peter HutchisonSenior Network Systems SpecialistCommented:
In the CSV file, the first row should be
Name,OU
0
QuazzieMAuthor Commented:
Still getting the same issue..

i used your command to export the list and then edit it to contain the single PC for testing that i want to move but I get the same error

PS C:\> ./PC.ps1
Move-ADObject : Cannot find an object with identity: 'BALL-STOCK12' under: 'DC=
dev,DC=local'.
At C:\PC.ps1:5 char:18
+     Move-ADObject <<<<  -Identity $computer.Name -TargetPath $TargetOU -Confi
rm:$false
    + CategoryInfo          : ObjectNotFound: (BALL-STOCK12:ADObject) [Move-AD
   Object], ADIdentityNotFoundException
    + FullyQualifiedErrorId : Cannot find an object with identity: 'BALL-STOCK
   12' under: 'DC=dev,DC=local'.,Microsoft.ActiveDirectory.Management.Comm
  ands.MoveADObject

Ive attached the csv again that the command exported, excluding all the PC names bar the one for testing

Thanks again
AllWindows.csv
0
footechCommented:
For the -Identity parameter of Move-ADObject, you have to provide a distinguishedName or GUID for the AD object you want to move.  So, either your file needs to include that information (in which case your existing code would work), or you can do a search to find the object based on the name.


$TargetOU = "OU=TEST,OU=DEV_Computers,DC=dev,DC=local"

Import-Csv C:\PC.csv | Foreach-Object {
    Get-ADComputer $_.Name | 
     Move-ADObject -TargetPath $TargetOU -Confirm:$false
    Write-Host "$($_.Name) has been moved successfully" -ForegroundColor Green
}

Open in new window

2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
footechCommented:
https:#a40722859 should be accepted as the answer.  It is the only post that provides the needed info to the Move-ADObject cmdlet and explains the reason for the previous errors encountered.
Given the .CSV format as provided by the author, it was verified to work.

https:#a40722552 was the only other one to come close to providing the proper identity to the Move-ADObject cmdlet, but it also had a number of syntax errors that would make it not work.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.