Move Computer to New OU Using PowerShell

I’m going to assume you have some tool you are using for PowerShell if you are reading this. PowerGUI is the one that I like the best currently. Once you’ve installed that, you research and download the Quest AD cmdlets. You should be set up at this point.

I had run into this issue in the past, albeit learning the hard way when I move “just a few” computers into the wrong OU. So this time I thought I would ask more questions and do more testing before I pulled the trigger. You can find where the journey begin by looking at this Forum Thread over at powergui.org.

To summarize, I wanted to get a list of computers and pipe it into PowerShell to move the computers to a new OU if the name matched exactly. The code that I had used before would move both Computer1 and Computer12. So I decided to ask the experts in the field and through a little trial and error I found a working solution. Here’s how it ended up working.

csv file looks like this:
object,target
computer1,red.bird.net/Red Computers/Big Building/Windows XP SP2 Test Group

PowerShell looks like this:

#take the -WhatIf off when you are ready to actually do it
$csv = Import-Csv C:\ps\PSTextFiles\yourFile.csv
foreach($computer in $csv){
# Display QADComputer for each item in the CSV object
Get-QADComputer -Identity $computer.object | Where-Object {$computer.object -eq $_.name} | Move-QADObject -To $computer.target -WhatIf
}

Dmitry Sotnikov (Blog|Twitter) goes even further to blog about how we should be explicit in what we are asking for.

Poshoholic (Blog|Twitter) mentions the post in his PowerGuide – PowerGUI Weekly News.

, ,