Need powershell help to move a list of computers into a corresponding list of OU's

I've dug in and found this much of the puzzle

$ComputersPath= Import-Csv -Path "C:\Users\Computernames\Computers.csv"
$TargetOU = "OU=funtimes,DC=test,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

Open in new window

This reads a .csv file full of computer names and moves them to the "$TargetOU", but I have another .csv file that contains a list of OU's that each machine needs to be moved to.
Samson AODAsked:
Who is Participating?
 
footechCommented:
Assuming that the .CSV has headers for "CompName" and "OU".  Each row in the .CSV needs to have both fields, so it is known which OU the particular computer should be moved to.
$ComputersPath= Import-Csv -Path "C:\Users\Computernames\Computers.csv"
foreach ($item in $ComputersPath){
    $computer = Get-ADComputer $item.CompName
    Move-ADObject -Identity $computer.DistinguishedName -TargetPath $item.OU -Confirm:$false
    Write-Host Computer account $computer.Name has been moved successfully
}

Open in new window

0
 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
can you post at least 2 lines of your 'container' of the OUs?  just need the name of the fields at the top.
0
 
Samson AODAuthor Commented:
Perfect this was exactly what I needed Thank you!
0
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.

All Courses

From novice to tech pro — start learning today.