Solved

Windows DOS Batch Profile Removal

Posted on 2010-09-21
3
330 Views
Last Modified: 2012-05-10
Hi there
I have found the following code on here which enables me to delete profiles very well. However i have hundreds of machines with duplicate profiles on and if i run this code it deletes the current users profile.
This is because everytime someone logs in to the machine an additional profile gets created and the code does not read this as a current user profile and attempts to delete it.
For example, Person A has a profile on the machine, they restart the machine and log in again, Person A then has a profile called Person A.WorkstationName. The code looks for the current user can't find it and deletes the profile Person A.WorkstationName which is currently being used.
If anyone could offer any advice it would be appreciated.
@echo off
for /f "tokens=*" %%a in ('dir c:\docume~1 /b /ad') do call :process "%%a"
goto :eof

:process
if [%1]==["Administrator"] goto :eof
if [%1]==["All Users"] goto :eof
if [%1]==["Default User"] goto :eof
if [%1]==["LocalService"] goto :eof
if [%1]==["NetworkService"] goto :eof
if [%1]==["Mits"] goto :eof
if [%1]==["Student"] goto :eof
if [%1]==["NWUSERNAME"] goto :eof
if [%1]==["USERNAME"] goto :eof
cd\docume~1
echo. About to remove profile %1
pause
rmdir %1 /s /q

Open in new window

0
Comment
Question by:TPS1979
  • 2
3 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 33723774
Does the %userprofile% variable get set correctly for these users?  If so you could add another exception by getting the directory name from the %userprofile% variable using a quick subroutine:

@echo off
call :getdir "%userprofile%"
echo %dirname%


:getdir
  set dirname=%~nx1
exit /b

You can also get the "root" profiles directory, at least based on the current profile so you don't have to hard code c:\docum... I have defined that as %rootdir%.

Have added /i to your directory checks so it makes them case insensitive and a /d to the cd command in case a machine has had the profiles moved to another drive etc.


@echo off
REM Obtain root directory of user profiles and current user profile name
call :getdir "%userprofile%"

REM run down each profile directory and call subroutine
for /f "tokens=*" %%a in ('dir "%rootdir%" /b /ad') do call :process "%%a"
goto :eof

:process
if /i [%1]==["Administrator"] goto :eof
if /i [%1]==["All Users"] goto :eof
if /i [%1]==["Default User"] goto :eof
if /i  [%1]==["LocalService"] goto :eof
if /i  [%1]==["NetworkService"] goto :eof
if /i  [%1]==["Mits"] goto :eof
if /i  [%1]==["Student"] goto :eof
if /i  [%1]==["NWUSERNAME"] goto :eof
if /i  [%1]==["USERNAME"] goto :eof
if /i  [%1]==["%dirname%"] goto :eof

cd /d "%rootdir%"
echo. About to remove profile %1
pause
rmdir %1 /s /q

exit /b

:getdir
  REM takes one parameter and strips the "filename/directoryname" and extension off that and returns it in %dirname%
  set dirname=%~nx1
  set rootdir=%~dp1
exit /b

Steve
0
 

Author Comment

by:TPS1979
ID: 33723966
Very impressed, worked a treat! Thank you.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33724000
No problem.... you just have to know some tricks sometimes.  Lots of info in the help texts of hte commands such as set /? for /? etc. and frankly learned most of the new stuff here over the years!

Of course if some users sets something like their profile path to c:\myname then the above could cause a few issues shall we say.... as in the root profiles dir would then be seen as c:\ ....

Good luck with anyway and just stick a question on here if it causes any issues

Steve
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question