Wednesday, September 1, 2010

Delete some local users and do a report

Today, support team need help to delete a lot of same local users on multiple servers which are in a workgroup (do not ask me why they do not use AD and domain....). And all these deletions must be report.
Like today I do not have really a lot of time to help them (too much projects to finish and manage today) I decided to do a 5 min batch script.

Here it is DeleteLocalUsers.cmd

set inputfile=users.txt
set result=result.txt
echo. >%result%
for /F %%i IN (%inputfile%) Do echo %%i >> %result% 2>>&1 & net user %%i /delete >> %result% 2>>&1


This batch use an input file named users.txt that containing users to delete like this:

aaaaaaa
bbbbbbb


No you just to run batch script to delete users (report file named result.txt)
Here user aaaaaa whas created before and bbbbbb does not exist

aaaaaa
The command completed successfully.

bbbbbb
The user name could not be found.

More help is available by typing NET HELPMSG 2221.


For better report, you can improve batch by testing if it's a successfull deletion by testing "successfully" with findstr command and errorlevel
If you need to execute this batch remotely on multiple computers, use psexec in for loop with a computers.txt file
(for example see in my blog in HP Onboard administrator and Citrix backup posts)

To download it

No comments: