VBScript: Move computer object to another OU via Command line parameter

zip MoveOU_0.0.3_www.coretech.dk.zip 881 bytes
Downloaded 2802 times

UPDATE: New improved script 0.0.3 uploaded. Thanks to Nico_ at Technet Forums!

Hello everyone

My collegue Michael Petersen, needed a script to move computers to another OU, after re-installing them via SCCM/ConfigMgr.

He have written a blog post to show how to use it in a Task Sequence: http://blog.coretech.dk/mip/using-ts-variables-when-running-a-script-under-a-different-account/

therefore i developed this small script

All you needs to do is to run it with the CN for the new OU (without the LDAP://) in the commandline like this:

cscript.exe MoveOU.vbs “OU=HQ,dc=woodgrovebank,dc=com”

' //***************************************************************************
' //***************************************************************************
' // ***** Script Header *****
' //
' // Solution:  ConfigMgr
' // File:      MoveComputerToCorrectOU.vbs
' // Author:	Jakob Gottlieb Svendsen, Coretech A/S. http://blog.coretech.dk
' // Purpose:   Move computer to the correct OU that remains in variable MachineObjectOU
' //		Run inside TS after install
' //
' // Usage:     MoveComputerToCorrectOU.vbs
' //
' //
' // CORETECH A/S History:
' // 0.0.1	JGS 17/12/2009  Created initial version.
' // 0.0.2	MIP 17/03/2009 	Added feature to add argument to script
' // 0.0.3  JGS 02/12/2010  Changed to ADSystemInfo for the DN retrieval, instead of a homemade function.
' //						Thanks to Nico_ at Technet Forums
' //
' // Customer History:
' //
' // ***** End Header *****
' //***************************************************************************
'//  Main routines

On Error Resume Next

'Get MachineObjectOU Value
Set wshNetwork = CreateObject("WScript.Network")
Set oFso = CreateObject("Scripting.FileSystemObject")
Set objSysInfo = CreateObject( "ADSystemInfo" )
Set ArgObj = WScript.Arguments

'Use first argument as target OU
strMachineObjectOU = ArgObj(0)
strComputerDN = objSysInfo.ComputerName

nComma = InStr(strComputerDN,",")
strCurrentOU = Mid(strComputerDN,nComma+1)
strComputerName = Left(strComputerDN,nComma - 1)

'If current ou is different than target OU. Move object
If UCase(strCurrentOU) <> UCase(strMachineObjectOU) Then
	Set objNewOU = GetObject("LDAP://" & strMachineObjectOU)
	Set objMoveComputer = objNewOU.MoveHere("LDAP://" & strComputerDN, strComputerName)
End If 

'//  End Script

Comments (64):

  1. Make says:

    Thanks, very useful !.

  2. Tony says:

    Thanks so much for posting this! The in-built task sequence steps for joining to domain and specifying OU don’t move to OU if the computer object already exists in AD and is already in an OU. A bit useless! I’ve incorporated this script into my task sequence and it works great. Cheers!

  3. […] solution is using this great VBSscript provided by Coretech.dk you can find it here:http://blog.coretech.dk/scripting/vbscript-move-computer-object-to-another-ou-via-command-line-param… The script will move the computer if it is not already in the specified OU, i was thinking of […]

  4. Great script! Thanks for sharing!
    I wrote a post about how I use it in a Task Seqeunce. http://ccmexec.com/?p=686
    Again, thanks!

  5. Erik says:

    Hi! I’m sure this is a great script but I cant’t manage to get it to run in my TS. If I run it manually from the computer it works fine. I also tried to copy it in the TS to the computer and run with command line, no luck. Any suggestions?

    • Hello Erik

      Since you have not put any error message / log output in your comment, i have no chance of knowing that problem you have

      i suggest to look at this article about running commands with variables in TS

      and also the above guide in Jörgen’s comment

      • Lubo says:

        Hi Jakob
        Script works by itself when running out of fully operational Win 7 OS, but when running from the task in a TS, it fails with this error “CScript Error: Can’t find script engine “VBScript” for script …

        I am using the same method depicted at:http://ccmexec.com/2010/12/move-computer-to-the-correct-ou-during-deployment/

        I should mention, I am using domain admin account to run the script out of task in my Windows 7 Image deployment Task Sequence ….not the computer system account. All my tasks executing VB Scripts run perfectly well in the same TS (while deploying the very same image) when running in the context of the system account

        Interesting: when I tried to deploy one of my test Win 7 images using the same TS script worked.
        Is my production Image broken?


        • Kalin Tashev says:

          Hi Lubo,

          Are you loading the user profile of the account used to run the script?
          I had similar failures with other VBS scripts I am running in another user context and they got resolved by loading the user’s profile.

  6. Erik says:

    Hi, thank you for your reply. No error messages, I have put this as the last step after installing SW. Some lines from the SMSTS log (If this is to much, let me know :):
    Variable name: _SMSTSRunCommandLineAsUser TSManager 24.06.2011 08:08:38 336 (0x0150)
    Variable name: SMSTSRunCommandLineUserName TSManager 24.06.2011 08:08:38 336 (0x0150)
    Variable name: SMSTSRunCommandLineUserPassword TSManager 24.06.2011 08:08:38 336 (0x0150)
    Action command line: smsswd.exe /run:P010003C cscript.exe MoveOU.vbs “OU=Desktop,OU=Computers,OU=xxxxxxxx,dc=xxxxx,dc=no” TSManager 24.06.2011 08:08:38 336 (0x0150)

    • erik says:

      Hi again,
      Finally got it working,:). Removed the dashes in the string and retyped them, must have been a copy error or a typo i guess. cscript.exe MoveOU.vbs “OU=Desktop,OU=Computers,OU=xxxxxxxx,dc=xxxxx,dc=no”

  7. Mike says:

    I am trying to test it now. But i do have a question. I have a staging OU, so i can run this script and move the client to staging OU “in case if client is already in AD” and then run the script as the very last step to move in to the perm OU?

  8. Mike says:

    When i run it manually i get no error msg but its not moving client to a new OU. When i try to run it from the TS the script is failing.
    cscript.exe “%SCRIPTROOT%\MoveOU.vbs “OU=Windows7,OU=***,OU=***,OU=***,DC=***,DC=***,DC=com”

  9. Bill says:

    Thanks for sharing this, it was exactly what I needed to resolve issues with GPO doing unwanted things during the OSD process. Works great for us

  10. Max says:

    MDT users: which part of the task sequence are you guys adding the vbs to?

  11. Ben says:

    This script runs great once I copy it to the local machine. The issue that I am having is that I am PXE booting a PC and it is pulling a MINIT… computername. How can I add a variable to use when searching AD instead of using the currenlty assigned computername?

    I am fairly new to scripting, but hacking away everyday.


  12. Ben says:


    To those who can not run it inside of a TS, I have resolved this by copying it to the local C:\, running it, and then deleted it from the C:\ in my TS.

  13. @Ben

    thnx for sharing the fix about the TS, before i got to reply.
    It is always a good idea to copy script to local PC if you are having problems

    about the computername

    just change
    strComputerDN = objSysInfo.ComputerName


    strComputerDN = “CN=MyPC,DC=Contoso,DC=com”

    unfortunately you will have to use the FQDN and not the NEtbios computername

    if you want to search by name instead you can implement at script like this one


  14. Ben says:


    thnx for the quick reply. I think that I worded my question wrong. I am wanting a popup box to ask me for the desired new name of the PC. Then I want the script to move that PC to a specific OU. Then that same name should be passed to the UDI for the computer name portion. Below is what I have so far. It does pass the name to the UDI, but it does not move the PC to the correct OU. All of this is being run in a TS.

    On Error Resume Next

    ‘Added variables
    Dim OSDComputerName

    ‘Popup to get OSDComputername
    strComputer = InputBox(“Enter Computer Name:”)
    Set myTSEnv = CreateObject(“Microsoft.SMS.TSEnvironment”)
    myTSEnv(“OSDComputerName”) = strComputer

    ‘Get MachineObjectOU Value
    Set wshNetwork = CreateObject(“WScript.Network”)
    Set oFso = CreateObject(“Scripting.FileSystemObject”)
    Set objSysInfo = CreateObject( “ADSystemInfo” )
    Set ArgObj = WScript.Arguments

    ‘Use first argument as target OU
    strMachineObjectOU = ArgObj(0)
    ‘strComputerDN = objSysInfo.ComputerName
    strComputerDN = objSysInfo.OSDComputerName

    nComma = InStr(strComputerDN,”,”)
    strCurrentOU = Mid(strComputerDN,nComma+1)
    strComputerName = Left(strComputerDN,nComma – 1)

    ‘If current ou is different than target OU. Move object
    If UCase(strCurrentOU) UCase(strMachineObjectOU) Then
    Set objNewOU = GetObject(“LDAP://” & strMachineObjectOU)
    Set objMoveComputer = objNewOU.MoveHere(“LDAP://” & strComputerDN, strComputerName)
    End If

  15. hello ben

    there are sevreal problems in your approach.

    1. You cannot show a messagebox or any other interaction with the user from a TS. It is not possible, since the TS does not run in the same context as the user (it runs in administrive context / local_system). at least that is what the documentation says, so i always keep from it, but maybe it does work on some machines?

    2. You are assigning a computername to myTSEnv(“OSDComputerName”) but using it as objSysInfo.OSDComputerName ?

    3. what kind of computername are you typing in ? is it DistinguishedName as required?

    – Jakob

  16. rdw says:

    I can’t seem to get this to work even from the command prompt. Using 0.0.3 version

    cscript.exe moveou.vbs “OU=XXXX,OU=XXX,OU=XX,DC=XXX,DC=XXX,DC=XXX”
    used dsquery computer to verify LDAP ifo.

    Any way to troubleshoot this with a log or something?

  17. jan says:

    I’m trying to use this script in a sccm TS. This TS is migrating a machine from XP to win7 and is using the MDT integrated DB to change the computername. The new computerobject is than put into the correct OU in AD.
    What I want to do is to use a script like this to move the old computerobject to a OU for migrated computers.
    I have noticed that the old computername is stored in the SCCM variable _SMSTSMachineName during the complete TS. So I am thinking of running this like:
    cssript.exe moveou.vbs “OU-….” %SMSTSMachineName%

    My skills in vb scripting are limited, but so far I have come up with the following change to the script:
    ‘Use first argument as target OU
    strMachineObjectOU = ArgObj(0)
    strComputerDN = ArgObj(1)

    So far this is not working for me. Any ideas

  18. Magnus says:


    You could use a HTA to pass variables from HTA to the TS.
    Heres a great post on it http://www.windows-noob.com/forums/index.php?/topic/3245-how-can-i-pass-values-from-the-hta-to-the-task-sequence/

    i also use a script to ask for a computer if the computer name begins with “minint” great when you install a new computer.

  19. Mike says:

    Does anyone has this working?
    I am trying to run this manually from the local script. Not getting any errors. However the workstation is not moving to a new OU.

    cscript.exe MoveOU.vbs “OU=Windows7,OU=**,OU=**,OU=**,DC=**,DC=**,DC=com”

  20. Mike says:

    Sorry for not giving more information.

    I am getting an error on the following line:
    Set objNewOU = GetObject(“LDAP://” & strMachineObjectOU)

    (null): A referral was returned from the server

    • Claus Codam Claus Codam says:

      Hey Mike,

      The error probably relates to incorrect DC statements. Please check that you haven’t made a typo or entered any wrong information.

      “A referral was returned from the server” basically means that the LDAP server you contacted, doesn’t recognize the requested DC path.

  21. Sundeep says:

    Hi Jakob

    Great script! Thanks for sharing!

    It works for me great when i am rebuilding a existing PC or Laptop which is already in Active Directory

    cant get it to work when doing bare metal PC. i am getting error 0x80004005 . can this script work on bare metal

    thanks for you help

  22. Ronald says:

    Can this script be run from a local machine account?

    Could it prompt for a domain user name/password with rights to move computers to OUs?

  23. Roel Janssens says:

    Thanks for this one!

  24. whelen says:

    Hi Jacob,
    I was face error on move desktop OU as using your script.
    MY workstationw as stuck in STaging OU….

    But i can do the manually move using your script.
    My Error as : Litetouch Deployment Failed, Return code = -2147467259 0x80004005
    Failed to run the action: Custom move acct to laptop OU.

    FYI, its working lastime…
    Dont know why recently failed to move at staging OU to laptop/Dekstop OU….

  25. whelen says:

    Hi Jakob,
    Thanks for your reply.
    your r right that code was access issue.
    I try to using other domain admin account also not luck.
    Funny was its sometime can move…sometimes fail…:”(

  26. whelen says:

    Hi Jakob,
    Is that any ways i can verified whether the task sequenceof run this step as the following account?
    Because i can manually move, but using it was failed…

  27. whelen says:

    Hi Jokob,
    I saw a reply as:
    Ben says:
    31st August 2011 at 21:37

    To those who can not run it inside of a TS, I have resolved this by copying it to the local C:\, running it, and then deleted it from the C:\ in my TS.

    How can i copy the MoveOU.vbs to C:\ in my ts?

  28. User says:

    Max says:
    30th August 2011 at 10:48
    MDT users: which part of the task sequence are you guys adding the vbs to?

    I never saw an answer to Max’s question. Which is the same one I have. The script works, but my policies (legal notice) are applied before the script is run. After I click okay to accept the legal notice the script moves the computer to a staging OU.


  29. Nike Air Max says:

    After going over a few of the blog posts on your site, I truly
    like your technique of blogging. I saved it to
    my bookmark website list and will be checking back in the near future.

    Take a look at my website as well and let me know what you

  30. Roel says:

    Each time I get an errorcode 1 and the machine is not moved within AD (permissions for account are correct).

    What does errorcode 1 mean?

  31. Phil says:

    Hi Jakob,
    What I have to change if I want to set the destination OU as a string inside the script and not as a command line parameter?
    I’ve done a lot of attemps but I cannot make it works.
    Thank you,

    • hello Phil

      These are the lines that handles the arguments from commandline:

      Set ArgObj = WScript.Arguments
      ‘Use first argument as target OU
      strMachineObjectOU = ArgObj(0)

      you can remove these lines and write your OU directly into the variable

      strMachineObjectOU = “OU=myOU,CN=mydomain,CN=com”

  32. Mike says:

    This is a popular script!!! So popular in fact that others are now claiming the work to be theirs!


  33. Ryan says:

    After the new machine is built, we require it to maintain the domain group memberships it had before the migration.
    What would be the best way to achieve this?

  34. JM says:

    Hi! We are deploying our computers with ZENworks and not SCCM, therefor I cannot specify the use of network credentials to run the script.

    Is there a way to hardcode in the script a username and password?



  35. Jared says:

    Wow, great script. Thanks.

    I do have a question though. I am using this script in my unattend.xml firstlogoncommands to change the computers OU. I’ve got it to work but in order for it to move the computer to the correct OU I have to login using my administrator account. After I login it magically moves to the new OU. I am deploying using WDS.

    Do you have any ideas on what the issue might be?

  36. Jack Fetter says:

    For those having issues with the script not working that are sure the script is running in the right place within the OSD and with an account that has proper permissions, check the “quote” characters. If you copied and pasted using the “Usage example” at the top of this page, the quote characters are not correct 😉

    cscript.exe MoveOU.vbs “OU=HQ,dc=woodgrovebank,dc=com”

    Quotes should be ” not ”

  37. basti says:

    Hey, i have tried to get your script working for days now…
    I am very frustrated about my TS. In my full installed Win 8.1, the script works perfectly! Only in my TS the Script won’t work….
    I have tested many solutions found in the web.

    – Testing various orders from TS
    – Doing Restart before or After the Script
    – Running the Script with a Batch file

    I have cleared some erros. but it doesnt work yet.

    I have copied the script locally and run it locally. Actually i just become this error:

    Expand a string: WinPEandFullOS TSManager 01.09.2015 10:05:32 2220 (0x08AC)
    Executing command line: smsswd.exe /run: cscript.exe %SystemDrive%\Windows\CCM\MoveOu.vbs “OU=A-A A,OU=BBB,OU=CCC,dc=DDD,dc=EEE” TSManager 01.09.2015 10:05:32 2220 (0x08AC)
    [ smsswd.exe ] InstallSoftware 01.09.2015 10:05:32 2624 (0x0A40)
    PackageID = ” InstallSoftware 01.09.2015 10:05:32 2624 (0x0A40)
    BaseVar = ”, ContinueOnError=” InstallSoftware 01.09.2015 10:05:32 2624 (0x0A40)
    ProgramName = ‘cscript.exe C:\Windows\CCM\MoveOu.vbs “OU=A-A A,OU=BBB,OU=CCC,dc=DDD,dc=EEE”‘ InstallSoftware 01.09.2015 10:05:32 2624 (0x0A40)
    SwdAction = ‘0001’ InstallSoftware 01.09.2015 10:05:32 2624 (0x0A40)
    Getting linked token InstallSoftware 01.09.2015 10:05:32 2624 (0x0A40)
    Get RunAs private desktop SMSTS-354609\SMSTSDesktop InstallSoftware 01.09.2015 10:05:35 2624 (0x0A40)
    Run command line under a user account InstallSoftware 01.09.2015 10:05:35 2624 (0x0A40)
    Command line for extension .exe is “%1” %* InstallSoftware 01.09.2015 10:05:35 2624 (0x0A40)
    Set command line: Run command line InstallSoftware 01.09.2015 10:05:35 2624 (0x0A40)
    Working dir ‘c:\Windows\CCM\’ InstallSoftware 01.09.2015 10:05:35 2624 (0x0A40)
    Executing command line: Run command line InstallSoftware 01.09.2015 10:05:35 2624 (0x0A40)
    Create a process under given user token InstallSoftware 01.09.2015 10:05:35 2624 (0x0A40)
    Process completed with exit code 1 InstallSoftware 01.09.2015 10:05:36 2624 (0x0A40)
    Command line returned 1 InstallSoftware 01.09.2015 10:05:36 2624 (0x0A40)
    Process completed with exit code 1 TSManager 01.09.2015 10:05:37 2220 (0x08AC)

    What can I do now? Can you help me out of this?

    Thanks in Advance :)

  38. Andrew says:

    This script works great when I run it manually. But I can’t for the life of me get it to work within a MDT TS. It always fails with return code -2147467259 0x80004005
    I’m starting to think it’s the “Run this step as the following account” function that isn’t working as it should.

    I know that the account I’m using under “Run this step as the following account” has the correct permissions. Because if I manually run CMD as a different user and use the same account and credentials that I use within the TS and just run “cscript.exe MoveOU.vbs “OU=myOU,CN=mydomain,CN=com”” it moves the computer object just fine.
    But just to be sure it’s not a permission problem I’ve even tried using the Domain Admin account in the TS to move the computer object, but alas…

    For those of you that actually got the script to work within a TS. Are you using MDT or SCCM and which version?
    I’m using MDT 2013 Update 1 build 8298.

    I’ve tried to copy the script to C:\MININT and have the TS run the script from that location and I’ve tried running it from %scriptroot% but I always get the same error.

  39. Emil says:


    I am trying to get this to work in a TS with UDI.

    I am using this command line: cscript.exe MoveOU.vbs “%OSDDomainOUName%”

    The script runs and TS finishes with no errors, but the computer is not moved to the correct OU… Am I doing anything wrong?

  40. Jeff says:

    hi Jakob

    thanks for the move computer object to another OU via Command line parameter.

    is there a script to delete computer object from AD via command line parameter?

  41. priMDT says:

    Does it work on Windows 10 ? It is not working for me.

  42. Surjeet says:

    Yes, it does work on Windows 10 and the only other thing that I’ve noticed with this script is that if you install applications and then later in the TS you use this script to move the machine – it would fail! So to get around it, I just used the script right after the drivers step followed by applications.

Leave a Reply