Jump to content

VBS script to delete Windows 7 user profiles when the PCs restarts


Recommended Posts

Posted

Hi

 

We use local profiles for Staff and students in our network. Please could someone post me a VBS script to delete the Windows 7 students user profiles when the PCs restarts . I am planning to create GPO and point the script under- Computer Configuration-Policies-Scripts-Shutdown.

Also want exception for Administrator, All users, default user, public, and default on the script.

The students profiles are eating the space on the C drive

 

Any suggestions will be great really helpful, Thanks in advance.

Posted

We use a batch:

 

@echo off
net stop WSearch
net stop TabletInputService
\\Domain.local\SYSVOL\Domain.local\scripts\delprof\delprof2.exe /ed:admin* /u
net start TabletInputService
net start WSearch

 

The reason we stop the above services is due to this

Posted (edited)

Hi Guys ,

 

We use Windows 7 and I want to give exceptions to the following folders (not to delete).I want only the students profiles to be deleted. Please let me know how I can add exceptions. Is it possible to test this batch batch file locally on a PC , before I could add the batch file path specified on the on the GPO - Computer configuration-Policies-Windows Settings-Scripts-Shutdown.

"All Users"

"Default User"

"Public"

"Default"

"Administrator"

 

Also we use only windows 7 and do I need add as per RobD

net stop WSearch

net stop TabletInputService

net start TabletInputService

net start WSearch

 

Please advise and Thanks for the help

Edited by genesis
Posted (edited)

EDIT: Going to give the option arthur uses a go instead though. (Why the hell are there so many ways to achieve the same goal, when one seems so much easier than the rest!?)

 

I have two scripts, one is a vbs script, and does everything properly but it seems to take a long time to run, the other is a batch script which runs through quite quickly. Both work, what effect the batch script has doing it the imporper way compared to the vbs that does it properly I'm not sure.

 

Both were obtained from other edugeek users or the internet so i obviously take no credit for these:

First the VBS script....

On Error Resume Next

Dim objFSO, objWMIService, strComputer, strFilter, intMaxProfileAge, colProfiles, objProfile, dtmLastUseTime

strComputer = "."
strFilter = "SID Like ""S-1-5-21%"" And Not LocalPath Like ""%Administrator%"""
intMaxProfileAge = 7

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2") 
Set colProfiles = objWMIService.ExecQuery("Select * From Win32_UserProfile Where " & strFilter)

If Not colProfiles Is Nothing Then
For Each objProfile in colProfiles
	dtmLastUseTime = CDate(Mid(objProfile.LastUseTime, 7, 2) & "/" & Mid(objProfile.LastUseTime, 5, 2) & "/" & Left(objProfile.LastUseTime, 4) & " " & Mid (objProfile.LastUseTime, 9, 2) & ":" & Mid(objProfile.LastUseTime, 11, 2) & ":" & Mid(objProfile.LastUseTime, 13, 2))
	MsgBox DateDiff("d", dtmLastUseTime, Date)
	If DateDiff("d", dtmLastUseTime, Date) > intMaxProfileAge Then
		Err.Clear
		
		objProfile.Delete_
		
		If Err.Number = -2147024809 Then
			'Profile in use, skipping.
		ElseIf Err.Number = -2147024751 Then
			objFSO.DeleteFolder objProfile.LocalPath, True
		ElseIf Err.Number <> 0 Then
			'"Error: " & Err.Number & ": " & Err.Description
		Else
			'Profile Deleted.
		End If
	End If
Next
End If

 

The batch script..

@ECHO OFF

title Windows 7 User Profile Cleaning

:: ----------
:: Add any users you wish to exclude from the wipe to the "userpreserve"
:: line below and separate them by commas.  Be careful - these are
:: case-sensitive.
:: ----------

:USERPRESERVE
set userpreserve="administrator,Administrator,All Users,UpdatusUser,Default,Default User,Public"

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
)

:: ----------
:: This is fairly optional - it's just something I added so
:: that I could keep an eye on the labs remotely to make
:: sure there weren't masses of critical errors with the
:: script failing.
::
:: If you don't want it, just comment-out or remove the
:: "net use" line.
::
:: If you do want it, then make the necessary modifications
:: to the net use to map an appropriate sharepoint.
:: ----------

::net use t: \\server\share

IF EXIST "t:\labreport.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:\labreport.txt"
net use t: /delete
GOTO EXIT

:EXIT
exit

:EOF

 

I'm yet to see any issues using the batch script option, I'd love to know why the vbs takes so long thouhg (literally like 10 minutes on shutdown to clear the profiles, or more!)

Edited by mrbios
Posted

@RobD's batch file will do exactly what you want (although it's probably worth adding /i onto the end to ignore errors).

 

@echo off
net stop WSearch
net stop TabletInputService
\\Domain.local\SYSVOL\Domain.local\scripts\delprof\delprof2.exe /ed:admin* /u /i
net start TabletInputService
net start WSearch

 

We use Windows 7 and I want to give exceptions to the following folders (not to delete)

The only folder you would need to add as an exclusion is the Administrator account since the rest are special folders and will be automatically excluded by delprof2.

Posted (edited)

Thank you so much, I am not very familiar with understanding the scripts. I have made changes the path for looking at reports ::net use t: \\MGMT-01\reports

Do I need to remove any of the below comments on the script which I have shown between the ***** lines or just copy as it is and paste on a notepad and create .bat

If I double click the created batch file on a local Win7 PC , will be able to see the results.

 

 

*******************************************************************

 

title Windows 7 User Profile Cleaning

 

:: ----------

:: Add any users you wish to exclude from the wipe to the "userpreserve"

:: line below and separate them by commas. Be careful - these are

:: case-sensitive.

:: ----------

 

 

:: ----------

:: This is fairly optional - it's just something I added so

:: that I could keep an eye on the labs remotely to make

:: sure there weren't masses of critical errors with the

:: script failing.

::

:: If you don't want it, just comment-out or remove the

:: "net use" line.

::

:: If you do want it, then make the necessary modifications

:: to the net use to map an appropriate sharepoint.

:: ----------

**************************************************************************

 

ECHO OFF

title Windows 7 User Profile Cleaning

:: ----------

:: Add any users you wish to exclude from the wipe to the "userpreserve"

:: line below and separate them by commas. Be careful - these are

:: case-sensitive.

:: ----------

:USERPRESERVE

set userpreserve="administrator,Administrator,All Users,UpdatusUser,Default,Default User,Public"

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

)

:: ----------

:: This is fairly optional - it's just something I added so

:: that I could keep an eye on the labs remotely to make

:: sure there weren't masses of critical errors with the

:: script failing.

::

:: If you don't want it, just comment-out or remove the

:: "net use" line.

::

:: If you do want it, then make the necessary modifications

:: to the net use to map an appropriate sharepoint.

:: ----------

::net use t: \\MGMT-01\Reports

IF EXIST "t:\labreport.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:\labreport.txt"

net use t: /delete

GOTO EXIT

:EXIT

exit

:EOF

Edited by genesis

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...