Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Windows DOS Batch Profile Removal

Posted on 2010-09-21
3
Medium Priority
?
337 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month12 days, 16 hours left to enroll

971 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