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
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.

The script:

# Script to move the computer object in AD to the OU supplied as a variable
# 1st Example Command line Powershell.exe -NoProfile -ExecutionPolicy bypass -file MoveToOU.ps1 "%MachineObjectOU%"
# 2nd example command line Powershell.exe -NoProfile -ExecutionPolicy bypass -file MoveToOU.ps1 "OU=Desktop,OU=Computers,OU=Test,DC=Test,DC=Local"

$OU = $args[0]

try {
$CompDN = ([ADSISEARCHER]"sAMAccountName=$($env:COMPUTERNAME)$").FindOne().Path
$CompObj = [ADSI]"$CompDN"
catch {
$_.Exception.Message ; Exit 1

It can be downloaded here as well:

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!

NOTE: If you get an error like:

Exception calling “FindOne” with “0” argument(s): “Unknown error (0x80005000)”
Then check out this old post on an issue with the “Run this step as the following account” caused by .NET Framework 1.1. 

Thanks to my colleague Johan Schrewelius for the help.


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.