# Batch File Creation



## jonnygrim (Jul 14, 2003)

Hi,

Hoping omeone can help m.

I want to schedule a batch to run every night and remove the contents of the documents and settings folder except 3 domain user accounts, has anyone got a code for this or is it possible.

Thanks in advance./


----------



## jonnygrim (Jul 14, 2003)

sorry and also the local default user account needs to be untouched.

ta


----------



## TheOutcaste (Aug 8, 2007)

Need a bit more info:

Do you mean you want to clear the My Documents folders for all users except the 3 domain accounts, or do you want to actually delete all user accounts but the 3 domain users?

If you don't want to delete the users, do you also need to clear the /temp/browser cache/cookies/java cache etc folders?

Are there only Domain Accounts, or both Domain and Local accounts?

Besides the Default user folder under Documents and Settings, you should also leave the Administrator (default name for builtin Admin account, could have been renamed), AllUsers, LocalService, and NetworkService folders untouched as well.

Jerry


----------



## jonnygrim (Jul 14, 2003)

Hi,

Basically.

I have a terminal server which has a process that removes there own user profile when they log off. But i'm find that if they disconnect the session, it leaves the profile on a local server. So what I aim to do is at midnight have a batch file that removes all local user profiles on the server except admin, and default user.

Hope this helps.

thanks


----------



## TheOutcaste (Aug 8, 2007)

In the following examples I have added an *ECHO* command on the lines that actually delete the user account and user files. If you mistype a username in the *deletethese.txt* file or especially the *keepusers.txt* file, you could delete the wrong account.
_*
Windows will let you delete the account you are currently logged into from the command line.*_

If an account name is not in the keepusers.txt file it *WILL* be deleted, so test these carefully first. Once you are sure it's not going to delete the wrong thing, remove the red *ECHO* from the two lines.

If you know the user names that are created during the day it's easy:

```
@Echo Off
:: Set the path to the file containing the names to delete here
Set _F1=c:\Test\deletethese.txt
Set _F3=C:\Documents and Settings\
For /F "tokens=*" %%I in (%_F1%) Do (
    Echo Deleting User Account %%I
    [B][COLOR=Red]ECHO[/COLOR][/B] Net User "%%I" /Delete
    [B][COLOR=Red]ECHO[/COLOR][/B] If EXIST "%_F3%%%I" Echo Deleting User files for %%I& RD "%_F3%%%I" /S /Q
    )
Set _F3=
Del deletethese.txt
```
As each user logs on you just need to add that user name to the *deletethese.txt* file, *echo %username%>deletethese.txt* will do it. Multiple entries for the same name are OK, as are entries for accounts that get deleted when they log out properly. Windows will simply say it can't find the name and go to the next one. If you have dozens of names that are deleted properly and only one or two that you need this script for, it might be worth removing the name from the file in the logoff script.

If you are running WinXP or newer you can use *wmic* to list all user accounts, and delete the ones you don't want by keeping them in a "keepusers" file:

```
@Echo Off
Set _F1=%temp%\}tmp1{
Set _F2=C:\Test\keepusers.txt
:: Set path as needed for OS
Set _F3=C:\Documents and Settings\
If EXIST %_F1% Del %_F1%
For /F "tokens=2* skip=2 delims=," %%I In ('wmic useraccount get name^,status /FORMAT:CSV') Do Echo %%I>>%_F1%
For /F "tokens=*" %%I In ('findstr /IV /G:"%_F2%" %_F1%') Do (
    Echo Deleting User Account %%I
    [B][COLOR=Red]ECHO[/COLOR][/B] Net User "%%I" /Delete
    [B][COLOR=Red]ECHO [/COLOR][/B]If EXIST "%_F3%%%I" Echo Deleting User files for %%I& RD "%_F3%%%I" /S /Q
    )
If EXIST %_F1% Del %_F1%
For /F "tokens=1 delims==" %%I In ('Set _') do Set %%I=
```
Keepusers.txt would contain the following at a minimum:
*Administrator
Guest*

WinXP also has the following:

*HelpAssistant
SUPPORT_388945a0
*
Add any other accounts you want to keep to the list such as *ASPNET* or any *IUSR* accounts on the system. Any user account *NOT* in this file will be deleted.

Just run *Net User* from a Command Prompt to see what is there. Note that the net user command won't let you delete the Administrator or Guest account, but will let you delete the account you are currently logged into..

If you don't have *wmic* (Win2K) or don't want to have it running on the system, you can use *Net User* to read the names:

```
@Echo Off
Setlocal EnableDelayedExpansion
Set _F1=C:\Test\}tmp1{
Set _F2=C:\Test\keepusers.txt
Set _F3=C:\Documents and Settings\
Set _os=
ver | find "2000" > Nul
If %ERRORLEVEL% == 0 Set _os=.
If EXIST %_F1% Del %_F1%
For /F "tokens=1* skip=4 delims=" %%I In ('Net User') Do (
    Set _NetUser=%%I
    If NOT "!_NetUser:~0,-1!"=="The command completed successfully%_os%" (
    Call :_RMTrail !_NetUser:~0,20!
    Set _User1=!_t1!
    Call :_RMTrail !_NetUser:~25,20!
    Set _User2=!_t1!
    Call :_RMTrail !_NetUser:~50,20!
    Set _User3=!_t1!
    If NOT "!_User1!"=="" Echo.!_User1!>>%_F1%
    If NOT "!_User2!"=="" Echo.!_User2!>>%_F1%
    If NOT "!_User3!"=="" Echo.!_User3!>>%_F1%
    ))
For /F "tokens=*" %%I In ('findstr /IV /G:"%_F2%" %_F1%') Do (
    Echo Deleting User Account %%I
    [B][COLOR=Red]ECHO[/COLOR][/B] Net User "%%I" /Delete
    [B][COLOR=Red]ECHO[/COLOR][/B] If EXIST "%_F3%%%I" Echo Deleting User files for %%I& RD "%_F3%%%I" /S /Q
    )
If EXIST %_F1% Del %_F1%
Endlocal
GOTO:EOF
::=============================================::
::                                                                                          ::
::                           Subroutines                                          ::
::                                                                                          ::
::=============================================::
:_RMTrail
:: Removes trailing Spaces from a passed variable
:: Arguments Name of variable to be trimmed
:: Returns value in _t1
::
Set _t1=%*
GOTO:EOF
```
This uses the same keepusers.txt file as example#2.
This version will not be able to delete user accounts that are created with trailing spaces in the name -- Win2K and WinXP will both let you create accounts like that, but you can't log into them.

HTH

Jerry


----------



## Techmonkeys (Feb 10, 2005)

Wow this massivley over complicated.

There is a tool from MS called Delprof, that will remove all local copies of domain profiles except those needed, such as the admin etc.

http://www.microsoft.com/downloads/...95-6063-4462-8150-360394e98e1e&displaylang=en

Then you simply make a batch file run the following:


```
delprof /q /r /d:7
```
We have this in our domain admin logon script, so each time we logon as the domain admin it removes any redundant profiles

*Note:* the above script removes any profiles that have not been used in the last 7 days, but leaves others. You can change this by getting rid of the /d:7 switch


----------



## TheOutcaste (Aug 8, 2007)

Delprof.exe can be useful; it will ignore the system accounts Asp.net, Guest, HelpAssistant and Support_388945a0. But, if you run it without the D switch, it will delete every account that is not logged on *including* the built-in Administrator account (if you happen to run it from a different account, say a domain account). Windows will re-create the account when you log into it again, but your documents and settings will be gone. But then my batch file will do the same if you mess up the keepusers.txt file.

Running it as a scheduled task using the /d switch with the Administrators credentials will keep the Administrators account from being deleted, but it won't delete any accounts created in the last 24 hours which is what the OP wanted. If leaving the profiles an extra day will work and he wants no user profiles but the one the task runs under, this would be a good solution for XP or Win2K.

Delprof.exe won't work on Vista -- it refuses to even install on my Vista Ultimate SP1, and what I've read on the web says it will happily corrupt the registry if you do manage to run it.

At least the OP has several options to choose from.

Jerry


----------

