Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have an OU with around 2500 computers. I need to move 580 of them to another OU. Is there a way to do this with a batch file? I have a text file with the computernames (not the FQDN).

share|improve this question

4 Answers 4

FOR /f %%i in (C:\path\to\textfile.txt) do (

  dsquery computer -name %%i | dsmove -newparent OU=newOU,DC=domain,DC=com

)

That should take care of it for you.

edit: It's been a while since I've had to use dsmove :) It needs the DN of the object that you're moving, so I added the dsquery in front of a pipe to get the DN from the list and then pipe it to dsmove. This should work fine now for a list of just NetBIOS names.

share|improve this answer
    
+1 for doing it in 1 command –  Nixphoe Jun 1 '11 at 16:05

Assuming that you have the names of your comptuers in a text file and 1 on each line youc an run the following command to export the fqdn of them

FOR /f %%a in (file.txt) DO dsquery computer -name %%a >> fqdnfile.txt

Then run the following command to move them. Please use the echo command in lue of dsmove to test this first

FOR /f %%b in (fqdnfile.txt) DO dsmove %%b -newparent OU=newOU,DC=domain,DC=local

Test it out. Let me know if it doesn't work.

share|improve this answer
1  
You shouldn't need the FQDN for dsmove afaik. –  MDMarra Jun 1 '11 at 13:19
    
Sweet, never actually needed to move things in bulk before. But I've read much about the command. Good to know! Glad I got close to the same answer you did :) –  Nixphoe Jun 1 '11 at 13:22
1  
@Grey Tux You should run the script in cmd not powershell. –  jscott Jun 1 '11 at 13:36
1  
it must be %a instead of %%a –  Grey Tux Jun 1 '11 at 14:10
1  
Yeah, two % is if you're running it from a batch file. One % is if you're running it from the command line –  Nixphoe Jun 1 '11 at 14:18

A small error in Nixphoe's answer -- to vs do and a missing do

FOR /f %%a in (file.txt) do dsquery computer -name %%a >> fqdnfile.txt

FOR /f %%b in (fqdnfile.txt) do dsmove %%b -newparent OU=newOU,DC=domain,DC=local
share|improve this answer
1  
Welome to ServerFault. Please feel free to make suggested edits to existing posts. These suggestions will be vetted by the community for approval. –  jscott Nov 7 '13 at 11:12

Little step by step for new admins: Get file.txt where computer names are listed there one per line.

PC-001
PC-002
PC-003

Open CMD and make sure your line is indicating to be at the same location as your file.txt then paste the line below

FOR /f %a in (file.txt) DO dsquery computer -name %a >> fqdnfile.txt

This will create a file fqdnfile.txt in the same location as your file.txt

This will parse fqdnfile.txt with Distinguish names (one per line)

NOTE: that you should use double %% for BAT file and single % for CMD

Then run the line below.

FOR /f %b in (fqdnfile.txt) DO dsmove %b -newparent "OU=COMPUTERS,OU=My Bus,DC=Domain,DC=com"

NOTE: Your distinguish name should be enclosed in "" if you have spaces in names, e.g. My Bus

RUN the line below to check if everything was moved correctly

FOR /f %a in (file.txt) DO dsquery computer -name %a >> location.txt
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.