Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I delete all the "Account Unknown" profiles using a batch file?

Posted on 2007-11-29
6
Medium Priority
?
3,653 Views
Last Modified: 2008-06-19
Greetings -

Is anyone aware of a way to delete all the "Account Unknown" user profiles listed on a workstation via a batch file?

Perhaps I'm drawing a blank but I can't seem to think of a way to do it from the command line.  Please do not point out the "delprof.exe" utility from Microsoft as it is not designed to do this and won't recognize anything listed as "Account Unknown".

The domain is not using roaming profiles.  These "Account Unknown" profiles are remnants of some old local accounts that used to exist that were not deleted completely.  I basically want to delete anything that is an "Account Unknown" profile.

Thanks!
0
Comment
Question by:amendala
[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
  • 3
6 Comments
 
LVL 13

Expert Comment

by:haim96
ID: 20376960
0
 

Author Comment

by:amendala
ID: 20376995
Yes, I did.  But unfortunately, it doesn't apply to what I'm after.  That thread relates to removing user profile directories that are beyond a certain age.

I am not concerned about age or any other factors other than that if it is listed as "Account Unknown", I want it deleted.
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 20377879
Here's my attempt.  Please test this thoroughly before using it on a production system.

Once you are certain it is doing what you want, remove the echo commands from lines 28 and 29.  It will not actually delete anything until you do so.


@echo off
setlocal
 
set reglist=HKLM\software\microsoft\windows nt\currentversion\profilelist
 
for /F "tokens=* usebackq" %%G in (`reg query "%reglist%"`) do (
 for /F "tokens=* usebackq skip=4" %%H in (`reg query "%%G" /v ProfileImagePath 2^>NUL`) do call :_process1 "%%G" "%%H"
)
goto :_end
 
:_process1
for /F "tokens=1,2 delims=." %%J in ("%~n2") do call :_process2 "%~1" "%%J" "%%K"
goto :eof
 
:_process2
set active=
if "%~3"=="" (set prof=%~2) else (set prof=%~2.%~3)
dir >NUL
net user %2 > NUL 2>&1
if "%errorlevel%"=="0" set active=true
dir >NUL
net user %2 /domain > NUL 2>&1
if "%errorlevel%"=="0" set active=true
if /I "%~2"=="systemprofile" set active=true
if /I "%~2"=="LocalService" set active=true
if /I "%~2"=="NetworkService" set active=true
if not defined active (
 echo reg delete %1
 echo rd /q /s "%allusersprofile%\..\%prof%"
)
goto :eof
 
:_end
endlocal

Open in new window

0
 
LVL 38

Expert Comment

by:Shift-3
ID: 20378180
Some corrections and comments added.


@echo off
setlocal
 
REM Registry location of the profile list shown in System properties.
set reglist=HKLM\software\microsoft\windows nt\currentversion\profilelist
 
REM Queries the profile list for each profile's path
for /F "tokens=* usebackq" %%G in (`reg query "%reglist%"`) do (
 for /F "tokens=* usebackq skip=4" %%H in (`reg query "%%G" /v ProfileImagePath 2^>NUL`) do call :_process1 "%%G" "%%H"
)
goto :_end
 
:_process1
REM Subroutine to strip out the user name from the profile path.
for /F "tokens=1,2 delims=." %%J in ("%~n2") do call :_process2 "%~1" "%%J" "%%K"
goto :eof
 
:_process2
set active=
REM Corrects for user.domain profile paths
if "%~3"=="" (set prof=%~2) else (set prof=%~2.%~3)
REM Runs the net user command to determine if the username is valid locally
dir >NUL
net user %2 > NUL 2>&1
if errorlevel 0 set active=true
REM Runs the net user command to determine if the username is valid on the domain
dir >NUL
net user %2 /domain > NUL 2>&1
if errorlevel 0 set active=true
REM Checks for the built-in profiles
if /I "%~2"=="systemprofile" set active=true
if /I "%~2"=="LocalService" set active=true
if /I "%~2"=="NetworkService" set active=true
REM If none of the above conditions are met, deletes the profile's registry entry and folder.
if not defined active (
 reg delete %1 /f
 rd /q /s "%allusersprofile%\..\%prof%"
)
goto :eof
 
:_end
endlocal

Open in new window

0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 2000 total points
ID: 20378197
Corrections to the corrections.


@echo off
setlocal
 
REM Registry location of the profile list shown in System properties.
set reglist=HKLM\software\microsoft\windows nt\currentversion\profilelist
 
REM Queries the profile list for each profile's path
for /F "tokens=* usebackq" %%G in (`reg query "%reglist%"`) do (
 for /F "tokens=* usebackq skip=4" %%H in (`reg query "%%G" /v ProfileImagePath 2^>NUL`) do call :_process1 "%%G" "%%H"
)
goto :_end
 
:_process1
REM Subroutine to strip out the user name from the profile path.
for /F "tokens=1,2 delims=." %%J in ("%~n2") do call :_process2 "%~1" "%%J" "%%K"
goto :eof
 
:_process2
set active=
REM Corrects for user.domain profile paths
if "%~3"=="" (set prof=%~2) else (set prof=%~2.%~3)
REM Runs the net user command to determine if the username is valid locally
dir >NUL
net user %2 > NUL 2>&1
if "%errorlevel%"=="0" set active=true
REM Runs the net user command to determine if the username is valid on the domain
dir >NUL
net user %2 /domain > NUL 2>&1
if "%errorlevel%"=="0" set active=true
REM Checks for the built-in profiles
if /I "%~2"=="systemprofile" set active=true
if /I "%~2"=="LocalService" set active=true
if /I "%~2"=="NetworkService" set active=true
REM If none of the above conditions are met, deletes the profile's registry entry and folder.
if not defined active (
 echo reg delete %1 /f
 echo rd /q /s "%allusersprofile%\..\%prof%"
)
goto :eof
 
:_end
endlocal

Open in new window

0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

662 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