Solved

Windows DOS Batch Profile Removal

Posted on 2010-09-21
3
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

690 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