Restart The System Using An Existing Batch Script And Run The Batch Script When System Restarts

Hi to all,

I have a batch script which allows me to delete all user profiles including their registry counterparts except for the users stated in the script.

I have applied the user delete batch script to Computer Configuration>Policies>Windows Settings>Scripts>Shutdown and I have also created a scheduled task in the same GPO, under User Configuration>Preferences>Scheduled Tasks, using another batch file which notifies the user that they have "x" amount of time to save their files and which then restarts the system after the allotted time period has passed. -- This does not seem to work despite the fact that I have set the task to run at a time much later in the day and on a specific day of the week.

NOTE: I am testing this policy on the same day and I am changing the time on it and updating the test system to which this policy is being applied to every time I make a change in the policy.

Would the problem reside in the fact that I have the user profile delete script and the scheduled task rule which points to the restart script, running in the same GPO?

Since the above GPO is not working for me, how would I enter a command in my user delete script which will notify the user that they have "x" amount of time to save their work and which then restarts the system after the allotted time has passed and runs the delete_user_script?

Here is the user_delete_script:

@ECHO OFF

:: --------------------------------------
::
:: Delete User Profiles In Windows 7
::
:: --------------------------------------

title Delete User Profiles In Windows 7

:: ----------
:: Any users that should not be deleted must be entered in the "userpreserve"
:: line. Separate each user by a comma. Characters are Case-Sensitive.
:: ----------

:USERPRESERVE
set userpreserve="Default, Public, UserProfile1, UserProfile2"

FOR /f "tokens=*" %%a IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECK "%%a"
GOTO VERIFY

:REGCHECK
set SPACECHECK=
FOR /f "tokens=3,4" %%b in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATH=%%b %%c
FOR /f "tokens=2" %%d in ('echo %USERREGPATH%') DO SET SPACECHECK=%%d
IF ["%SPACECHECK%"]==[""] GOTO REGCHECK2
GOTO USERCHECK

:REGCHECK2
FOR /f "tokens=3" %%g in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATH=%%g
GOTO USERCHECK

:USERCHECK
FOR /f "tokens=3 delims=\" %%e in ('echo %USERREGPATH%') DO SET USERREG=%%e
FOR /f "tokens=1 delims=." %%f IN ('echo %USERREG%') DO SET USERREGPARSE=%%f
ECHO %USERPRESERVE%|find /I "%USERREGPARSE%" > NUL
IF ERRORLEVEL=1 GOTO CLEAN
IF ERRORLEVEL=0 GOTO SKIP

:SKIP
ECHO Skipping user clean for %USERREG%
GOTO :EOF

:CLEAN
ECHO Cleaning user profile for %USERREG%
rmdir "C:\Users\%USERREG%" /s /q > NUL
ECHO Cleaning user registry for %USERREG%
reg delete %1 /f
IF EXIST "C:\Users\%USERREG%" GOTO RETRYCLEAN1
GOTO :EOF

:RETRYCLEAN1
ECHO Retrying clean of user profile %USERREG%
rmdir "C:\Users\%USERREG%" /s /q > NUL
IF EXIST "C:\Users\%USERREG%" GOTO RETRYCLEAN2
GOTO :EOF

:RETRYCLEAN2
ECHO Retrying clean of user profile %USERREG%
rmdir "C:\Users\%USERREG%" /s /q > NUL
GOTO :EOF

:VERIFY
FOR /f "tokens=*" %%g IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECKV "%%g"
GOTO REPORT

:REGCHECKV
set SPACECHECKV=
FOR /f "tokens=3,4" %%h in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATHV=%%h %%i
FOR /f "tokens=2" %%j in ('echo %USERREGPATHV%') DO SET SPACECHECKV=%%j
IF ["%SPACECHECKV%"]==[""] GOTO REGCHECKV2
GOTO USERCHECKV

:REGCHECKV2
FOR /f "tokens=3" %%k in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATHV=%%k
GOTO USERCHECKV

:USERCHECKV
FOR /f "tokens=3 delims=\" %%l in ('echo %USERREGPATHV%') DO SET USERREGV=%%l
FOR /f "tokens=1 delims=." %%m IN ('echo %USERREGV%') DO SET USERREGPARSEV=%%m
ECHO %USERPRESERVE%|find /I "%USERREGPARSEV%" > NUL
IF ERRORLEVEL=1 GOTO VERIFYERROR
IF ERRORLEVEL=0 GOTO :EOF

:VERIFYERROR
SET USERERROR=YES
GOTO :EOF

:REPORT
IF [%USERERROR%]==[YES] (
		set RESULT=FAILURE
)		ELSE (
		set RESULT=SUCCESS
)

:: ----------
:: Error reports will be sent to cannon.
:: ----------

net use x: \\cannon\server_errors

IF EXIST "x:\UserDeletionReport.txt" (
	GOTO REPORTGEN
) ELSE (
	GOTO EXIT
)

:REPORTGEN
FOR /F "tokens=*" %%n in ('echo %date:~10,4%-%date:~4,2%-%date:~7,2% %time:~0,2%-%time:~3,2%-%time:~6,2%') DO SET TDATETIME=%%n
FOR /F "tokens=14" %%o in ('ipconfig^|find "IPv4 Address"') DO SET IPNUMBER=%%o
ECHO. %RESULT%	%COMPUTERNAME%	%IPNUMBER%	%TDATETIME%>>"t:\UserDeletionReport.txt"
net use t: /delete
GOTO EXIT

:EXIT
exit

:EOF
Thanks in advance,

February 16th, 2015 4:21pm

Just set Group Policy to remove user profiles on logoff.

Free Windows Admin Tool Kit Click here and download it now
February 16th, 2015 4:25pm

Would this policy also delete the registry SIDs that are associated with the profiles? Wouldn't this policy also delete the domain admin profiles? If so, that is not what I want and I guess I would have to prevent the GPO from applying to the domain security group in which those profiles reside in.

Also, will this policy leave the Default, Public, and local admin profiles alone?

Thanks jrv!

February 16th, 2015 4:34pm

That is what it is for.
Free Windows Admin Tool Kit Click here and download it now
February 16th, 2015 5:17pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics