0

I need to remove 1000 computers from AD. I tried the script below, but it's not working. When I used dsmod to disable computers, in the same script, it works.

@echo off
set ComputerList=C:\Users\a058059x\Desktop\Machines.txt
for /f "delims=" %%a in ('type "%ComputerList%"') do (
    echo Processing %%a ...
    dsquery computer -name "%%a" | dsrm
)

Can anyone help me to correct the error in this script?

jscott
  • 24,484
  • 8
  • 79
  • 100
karthick
  • 327
  • 1
  • 4
  • 12
  • 2
    Could you share what you mean by "not working"? Any error messages? By default, `dsrm` will prompt to remove each object. Using `dsrm -noprompt` enables non-interactive mode. – jscott Jan 21 '14 at 11:32
  • Tip: Instead of using having the `for` command type the text file, why not just put file name in? I've occasionally run into encoding issues, when typing a text file instead of reading directly. Try `for /f "delims=" %%a in (%ComputerList%)` or `for /f "delims= usebackq" %%a in ("%ComputerList%")` if you need to support paths with spaces. – abstrask Jan 21 '14 at 11:39
  • i am getting dsrm failed:'-name' is an unknown parameter. this error. when run via command line its working. – karthick Jan 21 '14 at 12:41
  • when i run this command @echo off set ComputerList=C:\Users\a058059x\Desktop\Machines.txt for /f "delims=" %%a in ("%ComputerList%") do ( echo Processing %%a ... dsquery computer -name "%%a" | dsrm -noprompt >> result.txt ) i am getting the error : Target object for this command' is missing... – karthick Jan 21 '14 at 13:17
  • 2
    Is here a reason you're using the ds tools and batch instead of PowerShell with the Active Directory module? – MDMarra Jan 21 '14 at 13:21

1 Answers1

0

Have you considered using powershell to accomplish this? The command is Remove-ADComputer.

Your command would look like this:

Get-Content COMPUTERLIST.txt | Get-Computer | Remove-ADComputer -recursive

assuming you have ADWS installed