Solved

Windows DOS Batch Profile Removal

Posted on 2010-09-21
3
328 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now