JSI Tip 7445. How do I remove deleted users home folders, and/or profile folders?

If the previous administrators were sloppy, and did not delete a user's folders when the user was deleted, you might have many unused folders remaining on your profile and home shares, or on a local hard drive.

Using the DSQUERY USER command, if have scripted UsrClnUp.bat to remove all deleted users folders in a path that you specify.

The syntax for using UsrClnUp.bat is:

UsrClnUp ServerPath1 \[ServerPath2 ..... ServerPathN\]

where the ServerPath points to the parent folder, or share, that you wish to purge of deleted user folders.


usrclnup \\DC1\HomeShare \\DC2\ProfileShare

usrclnup "C:\Documents and Settings"

for /f "Tokens=*" %u in ('usrclnup \\DC1\HomeShare \\DC2\ProfileShare') do @echo %u>>Report.txt
The above is typed at a CMD prompt and redirects the console output to a file.

pipe "%userprofile%\My Documents\report.txt" c:\folder\usrclnup.bat \\DC1\HomeShare \\DC2\ProfileShare

usrclnup \\DC1\HomeShare \\DC2\ProfileShare 2>>&1 | mtee "%userprofile%\My Documents\report.txt"

NOTE: UsrClnUp.bat assumes that a user's folder is named the same as their user name.

NOTE: You must run UsrClnUp.bat on a Windows XP workstation, or on a Windows Server 2003 computer.

UsrClnUp.bat contains:

@echo off
If \{%1\}

\{\} @echo Syntax: UsrClnUp ServerPath1 \[ServerPath2 ..... ServerPathN\]&goto :EOF setlocal :loop if \{%1\}

\{\} goto :Finish set sp=%1 @echo Begin processing the %sp% path. set up=%sp:"=% for /f "Tokens=*" %%h in ('dir /b /ad %sp%') do set user=%%h&call :chkusr shift goto loop :Finish endlocal goto :EOF :chkusr set dn="N" for /f "Tokens=*" %%x in ('dsquery user -samid "%user%" -o dn') do set dn=%%x if %dn% NEQ "N" goto :EOF RD /s /q "%up%\%user%" @echo "%up%\%user%" deleted.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.