Move the computer to the correct OU during OSD – PS version

I wrote a blog post way back(2010) on how to move a computer to another OU during OS Deployment https://ccmexec.com/2010/12/move-computer-to-the-correct-ou-during-deployment/.
Could be that you want to move it when you upgrade to Windows 10 or if you reinstall a computer and it already exist in an different OU then it will not be moved automatically. The script is still being used, downloaded and commented on. So here is an updated blog post with a PowerShell script that does the move, if you don’t want to use a webservice which there are couple of on Technet Gallery.

The script:
# Script to move the computer object in AD to the OU supplied as a variable
# Example Command line Powershell.exe -NoProfile -ExecutionPolicy bypass -file MoveToOU.ps1 "%MachineObjectOU%"

$OU = $args[0]

try {
$CompDN = ([ADSISEARCHER]”sAMAccountName=$($env:COMPUTERNAME)$”).FindOne().Path
$CompObj = [ADSI]”$CompDN”
$CompObj.psbase.MoveTo([ADSI]”LDAP://$($OU)”)
}
catch {
$_.Exception.Message ; Exit 1
}

It can be downloaded here as well: https://gallery.technet.microsoft.com/

Save the script to a file in your package source folder for the script.

  1. Add the script to a package you replicate to all DPs
  2. In the Task Sequence add the following Run Command Line task (Make sure to add it after the “Setup Windows and Configuration Manager Client step”)
  3. Use a User Account with permission in the Active Directory to perform the task.

In this example the %MachineObjectOU% variable is used which can be set by MDT for example.

As simple as that and you are done!

Thanks to my colleague Johan Schrewelius for the help.

10 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.