(view source code of rmuserid.bat as plain text)
@ECHO OFF
IF "%OS%"=="Windows_NT" SETLOCAL
:: Windows 2000 or later required
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
VER | FIND "Windows NT" >NUL && GOTO Syntax
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
:: Modify the variables below to match your own environment! ::
:: These sample values assume that: ::
:: [1] homedirs are located in \\MYHOMESRV\Users$\ ::
:: [2] profiles are located in \\MYPROFSRV\Profiles$\ and ::
:: [3] TS profiles are located in \\MYTSPRSRV\TsProfiles$ ::
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
SET DomainCtr=MYDOMCTR
SET HomeDirSrv=MYHOMESRV
SET HomeDirRootShare=Users$
SET ProfileSrv=MYPROFSRV
SET ProfileRootShare=Profiles$
SET TsProfileSrv=MYTSPRSRV
SET TsProfileRootShare=TsProfiles$
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
:: Modify the variables above to match your own environment! ::
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
:: Check command line arguments
IF "%~1"=="" GOTO Syntax
IF NOT "%~2"=="" GOTO Syntax
CLS
ECHO.
ECHO DISCLAIMER
ECHO.
ECHO This is a very powerful and potentially harmful tool.
ECHO.
ECHO Though every effort has been made to make this batch file safe and fool
ECHO proof, it has been tested in one single office network environment only.
ECHO There is no guarantee it will work or be safe in your environment.
ECHO Examine the source code thoroughly before putting it to use.
ECHO Make sure you have a recent and full system state backup available of
ECHO your domain controllers before putting this script to use, and test it
ECHO in a separate test domain first.
ECHO Use this batch file entirely at your own risk.
ECHO.
ECHO I have read and understood the source code, and I accept full responsibility
SET Answer=N
SET /P Answer=for any damage that may result from the use of this script (y/N)
IF /I NOT "%Answer%"=="Y" GOTO Aborted
:: Domain Admin permissions required
NET.EXE USER "%UserName%" /DOMAIN 2>NUL | FIND.EXE "*Domain Admins" >NUL || GOTO Syntax
:: No wildcards or whitespace allowed in command line arguments
ECHO "%~1" | FINDSTR.EXE /R /C:"[/?*& ]" >NUL 2>&1 && GOTO Syntax
:: Check if all tools used by this batch file are available
DSGET.EXE /? >NUL 2>&1 || GOTO Syntax
DSQUERY.EXE /? >NUL 2>&1 || GOTO Syntax
RMTSHARE.EXE /? >NUL 2>&1 || GOTO DownloadRK
TAKEOWN.EXE NUL 2>&1 | FIND.EXE /I "Successful" >NUL || GOTO DownloadRK
TSCMD.EXE /? 2>&1 | FIND.EXE "http://www.systemtools.com" >NUL || GOTO DownloadTsCmd
CALL OWN.BAT 2>&1 | FIND.EXE "http://www.robvanderwoude.com" >NUL || GOTO DownloadOwn
:: Confirm this is the correct account
ECHO.
ECHO Remove User ID . . .
ECHO.
DSQUERY.EXE User -samid %1 2>NUL | FIND.EXE "=" || GOTO Syntax
ECHO.
SET Answer=N
SET /P Answer=Do you really want to remove this account? (y/N)
ECHO.
IF /I NOT "%Answer%"=="Y" GOTO Aborted
:: Parse HomeDir and HomeShare if applicable
SET HomeDir=
SET HomeShare=
SET ShareDir=
FOR /F "tokens=1" %%A IN ('DSQUERY.EXE User -samid %1 2^>NUL ^| DSGET.EXE User -hmdir 2^>NUL ^| FINDSTR.EXE /R /I /C:"\\\\%HomeDirSrv%\\%1\$"') DO SET HomeDir=%%A
IF DEFINED HomeDir SET HomeShare=%HomeDir%
FOR /F "tokens=1" %%A IN ('DSQUERY.EXE User -samid %1 2^>NUL ^| DSGET.EXE User -hmdir 2^>NUL ^| FINDSTR.EXE /R /I /C:"\\\\%HomeDirSrv%\\%HomeDirRootShare:$=\$%\\%1"') DO SET HomeDir=%%A
IF NOT DEFINED HomeDir (
ECHO No homedir defined for this account.
GOTO Profile
)
:: Find the true path of a shared HomeDir if applicable
IF DEFINED HomeShare FOR /F "tokens=1*" %%A IN ('RMTSHARE.EXE %HomeShare% 2^>NUL ^| FINDSTR.EXE /R /B /I /C:"Path "') DO SET ShareDir=%%B
IF DEFINED ShareDir SET HomeDir=\\%HomeDirSrv%\%ShareDir::=$%
:: Confirm deletion
SET Answer=N
SET /P Answer=Delete home directory %HomeDir%? (y/N)
ECHO.
IF /I NOT "%Answer%"=="Y" GOTO Profile
IF DEFINED HomeShare (
ECHO Deleting home share %HomeShare% . . .
RMTSHARE.EXE %HomeShare% /DELETE
)
ECHO Deleting home directory %HomeDir% . . .
RD /S /Q %HomeDir%
:Profile
SET Profile=
FOR /F "tokens=1" %%A IN ('DSQUERY.EXE User -samid %1 2^>NUL ^| DSGET.EXE User -profile 2^>NUL ^| FINDSTR.EXE /R /I /C:"\\\\%ProfileSrv%\\%ProfileRootShare:$=\$%\\%1"') DO SET Profile=%%A
IF NOT DEFINED Profile (
ECHO No profile defined for this account.
GOTO TsProfile
)
IF NOT EXIST %Profile% (
ECHO Profile not found.
GOTO TsProfile
)
:: Confirm deletion
ECHO.
SET Answer=N
SET /P Answer=Delete profile directory %Profile%? (y/N)
ECHO.
IF /I NOT "%Answer%"=="Y" GOTO TsProfile
ECHO Deleting profile, please wait . . .
CALL OWN.BAT %Profile% ^>NUL 2^>^&1
RD /S /Q %Profile%
IF EXIST %Profile% (
FOR /F "tokens=*" %%A IN ('DIR /A-D /B /S "%Profile%\*.*"') DO (TAKEOWN.EXE "%%~A" >NUL 2>&1)
RD /S /Q %Profile%
)
ECHO Profile deleted.
:TsProfile
SET TsProfile=
FOR /F "tokens=*" %%A IN ('TSCMD.EXE %DomainCtr% %1 TerminalServerProfilePath 2^>NUL') DO SET TsProfile=%%A
IF NOT DEFINED TsProfile (
ECHO No Terminal Server profile defined for this account.
GOTO UserID
)
IF NOT EXIST %TsProfile% (
ECHO Terminal Server profile not found.
GOTO UserID
)
ECHO.
SET Answer=N
SET /P Answer=Delete Terminal Server profile %TsProfile%? (y/N)
ECHO.
IF /I NOT "%Answer%"=="Y" GOTO UserID
ECHO Deleting Terminal Server profile, please wait . . .
CALL OWN.BAT %TsProfile% ^>NUL 2^>^&1
RD /S /Q %TsProfile%
IF EXIST %TsProfile% (
FOR /F "tokens=*" %%A IN ('DIR /A-D /B /S "%TsProfile%\*.*"') DO (TAKEOWN.EXE "%%~A" >NUL 2>&1)
RD /S /Q %TsProfile%
)
ECHO Terminal Server profile deleted.
:UserID
ECHO.
SET Answer=N
SET /P Answer=Delete User ID %1? (y/N)
ECHO.
IF /I NOT "%Answer%"=="Y" GOTO End
NET USER %1 /DOMAIN /DEL
:: Done
GOTO End
:Aborted
ECHO.
ECHO Procedure aborted by user request.
GOTO End
:DownloadRK
ECHO.
ECHO Unable to find the Resource Kit tools RMTSHARE and TAKEOWN.
ECHO You need to download and install these tools in order to use this batch file.
ECHO.
SET Answer=N
SET /P Answer=Do you want to download these tools from the Microsoft website? (y/N)
IF /I "%Answer%"=="Y" START "Download NT4 Resource Kit" "http://www.microsoft.com/ntserver/nts/downloads/recommended/ntkit/"
ECHO.
ECHO Install the downloaded Resource Kit tools and try again.
GOTO End
:DownloadTsCmd
ECHO.
ECHO Unable to find the TSCMD tool.
ECHO You need to download and install this tool in order to use this batch file.
ECHO.
SET Answer=N
SET /P Answer=Do you want to download TSCMD from the SystemTools website? (y/N)
IF /I "%Answer%"=="Y" START "Download TSCMD" "http://www.systemtools.com/free_frame.htm"
ECHO.
ECHO Install TSCMD and try again.
GOTO End
:DownloadOwn
ECHO.
ECHO Unable to find OWN.BAT.
ECHO You need to download and install this tool in order to use this batch file.
ECHO.
SET Answer=N
SET /P Answer=Do you want to download OWN.BAT from this website? (y/N)
IF /I "%Answer%"=="Y" START "Download OWN.BAT" "http://www.robvanderwoude.com/files/owner.zip"
ECHO.
ECHO Unzip the downloaded OWNER.ZIP and copy OWN.BAT to a directory in the PATH.
ECHO Then try again.
GOTO End
:Syntax
CLS
ECHO.
ECHO RmUserID.bat, Version 0.52 beta
ECHO Remove user account and associated home directory, home share, and profiles
ECHO.
ECHO Usage: RMUSERID user_id
ECHO.
ECHO Where: user_id is the (SAM) user ID to be deleted
ECHO.
ECHO Notes: This batch file will ask for confirmation at each step of the process.
ECHO Please verify each name and path before pressing Y and Enter.
ECHO To run this script you need Domain Admin permissions.
ECHO You also need the following tools:
ECHO DSGET and DSQUERY (native in Windows Server 2003, will work
ECHO in Windows 2000), RMTSHARE and TAKEOWN (Windows NT 4 Server
ECHO Resource Kit), TSCMD (http://www.systemtools.com) and OWN.BAT
ECHO (http://www.robvanderwoude.com).
ECHO The batch file will prompt you for download if any of these is missing.
ECHO The batch file aborts if the user ID contains spaces or ampersands.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO.
PAUSE
ECHO.
ECHO DISCLAIMER
ECHO.
ECHO Though every effort has been made to make this batch file safe and fool
ECHO proof, it has been tested in one single office network environment only.
ECHO There is no guarantee it will work or be safe in your environment.
ECHO Examine the source code thoroughly before putting it to use.
ECHO Make sure you have a recent and full system state backup available of
ECHO your domain controllers before putting this script to use, and test it
ECHO in a separate test domain first.
ECHO Use this batch file entirely at your own risk.
ECHO.
PAUSE
:End
IF "%OS%"=="Windows_NT" ENDLOCAL
page last modified: 2024-04-16; loaded in 0.0082 seconds