Avatar of ivan rosa
ivan rosa
Flag for United States of America asked on

finding OU and deleting from domain Batch Script list COMPUTER and Another script move/delete them

Hello fellas,

Is there any way, anybody can help me to create  these three scripts below, I would appreciate a brilliant mind/s:

1) put in a computerlist.txt and will output.csv the computer name(column A) and current OU(column B) e.g.:
    PC NAME                 OU
workstation1           ACME

2)put in a computerlist.txt and it moves them to an specific OU

3)put in a computerlist.txt and deletes them from the domain
any thoughts?
Windows BatchActive DirectoryVB ScriptWindows NetworkingNetwork Operations

Avatar of undefined
Last Comment
ivan rosa

8/22/2022 - Mon

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

Moving a computer is quite similar, after finding the OU of the computer, use dsmove to move it to another OU:

setlocal EnableDelayedExpansion

for /F %%A in (computerlist.txt) do call :movecomputer %%A 

exit /b

set OU=
for /F  %%F in ('dsquery computer -name %1') do dsmove %%F -newparent ou=Marketing,dc=microsoft,dc=com

exit /b

Open in new window

Deleting is done with dsrm:

setlocal EnableDelayedExpansion

for /F %%A in (computerlist.txt) do call :removecomputer %%A 

exit /b

set OU=
for /F  %%F in ('dsquery computer -name %1') do dsrm %%F -noprompt

exit /b

Open in new window

As always, first test !
Will Szymkowski

If you want this to be done in Powershell, please let me know and i will assist.

ivan rosa


pretty close..., for your first, I created this one line script, which pretty much does what I need but I don't know how to split the result into the two columns

for /f "tokens=1" %a in (computerlist.txt) DO dsquery computer -name %a>>output.csv

so this is what CSV would look like e.g.

do you know how to split the result csv like so?
Column A                           Column B
cn=workstation1         OU=Computers,OU=Marketing,OU=Hollywood,OU=USA,DC=corp,DC=acme,DC=com
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

I wouldn't know how to do that; mainly because the output of de dsquery command is in fact a comma separated list, and for the csv file you actually want the first name, a comma, and then the rest of the line in quotes so the result is:

"cn=workstation1","OU=Computers,OU=Marketing,OU=Hollywood,OU=USA,DC=corp,DC=acme,DC=com "

Maybe with some tricks you could do that in one line, but i guess that would become quite unreadable. My script does the job and is pretty straightforward....
ivan rosa

This has actually helped me to combine my script with the above