Jump to content

Is there a script or GPO setting to clean out local profiles?


Recommended Posts

Posted

We are having an issue with some of our Windows 7 64 bit laptops failing to pick up network drives on logon. Traced it (I think) to local profiles remaining on the machine after shutdown.

 

Can I automate removal of the profiles on shutdown?

Posted (edited)

covered on other eg threads:

 

http://www.edugeek.net/forums/windows/4292-script-delete-profile-log-off-6.html

 

1. gpo settings

2. user hive profile cleanup service ( need to check if this still supports win 7 )

3. delprof2 ( delprof only goes as far as XP where as delprof2 afaik should work on win 7 )

 

wordpress article by @bio with ref to a gpo setting for win 7

 

Cleaning up student profiles on Windows XP machines « Knowledge for all (and free) !

 

Delprof2 – User Profile Deletion Tool | Helge Klein | Home of SetACL

Edited by mac_shinobi
  • Thanks 1
Posted
2. User Profile Hive Cleanup Service (need to check if this still supports win 7)

The "User Profile Service" built into Windows Vista and 7 makes the User Profile Hive Cleanup Service for XP redundant.

  • Thanks 2
Posted
One other thing to keep in mind is depending on your anti-virus installed, it may update the date modified on the user profile and prevent your automated profile deletion methods from being removed. There is a Microsoft Hotfix to correct it (KB983544). The GPO removed only on boot up, but you can use scripts to execute delprof2 to do it upon user logoff/logon. Personally, I use just the GPO as delprof2 requires to be ran as administrator, so when a student logs in they don't have the permissions. I have attempted to include credentials in the script but I couldn't get it to work very well. After I got the GPO to work, I quit trying to get delprof2 to work upon logon.
  • Thanks 1
Posted

This is the setting I use on our site to clean out profiles on every machine bar a few where it needs to keep the profiles and its done by GPO on all our windows 7 Machines (deleting profiles is not straightforward on 7 like it was on XP, because of the need to regedit).

 

 

Computer Configuration > Policies > Administrative Templates > Policy definitions (ADMX files) > System > User Profiles

 

Delete user profiles older than a specified number of days on system restart

Delete user profiles older than (days) 7

 

Anything going over 7 days gets binned here with this on our site, excellent for IT rooms and I apologise for not being able to credit who linked this on here originally that I used.

Posted
We are having an issue with some of our Windows 7 64 bit laptops failing to pick up network drives on logon. Traced it (I think) to local profiles remaining on the machine after shutdown.

 

Can I automate removal of the profiles on shutdown?

 

BATCH FILE....

 

@echo off

 

' DELETES LOCAL PROFILES ON WINDOWS 7

 

:START

FOR /f "tokens=*" %%a IN ('dir c:\USERS /b /ad') DO CALL :PATHCHECK "%%a"

GOTO REGISTRY

 

::The following is where you would put in the profile you wish to exclude from the wipe. Just copy/paste a line and make the appropriate revisions.

 

:PATHCHECK

IF /i [%1]==["Administrator"] GOTO :PATHSKIP

IF /i [%1]==["Admin"] GOTO :PATHSKIP

IF /i [%1]==["All Users"] GOTO :PATHSKIP

IF /i [%1]==["Default"] GOTO :PATHSKIP

IF /i [%1]==["Default user"] GOTO :PATHSKIP

IF /i [%1]==["public"] GOTO :PATHSKIP

GOTO PATHCLEAN

 

:PATHSKIP

ECHO. Skipping path clean for user %1

GOTO :EOF

 

:PATHCLEAN

ECHO. Cleaning profile for: %1

rmdir C:\USERS\%1 /s /q > NUL

IF EXIST "C:\USERS\%1" GOTO RETRYPATHFIRST

IF NOT EXIST "C:\USERS\%1" GOTO :EOF

 

:RETRYPATHFIRST

ECHO. Error cleaning profile for: %1 - Trying again.

rmdir C:\USERS\%1 /s /q > NUL

IF EXIST "C:\USERS\%1" GOTO RETRYPATHSECOND

IF NOT EXIST "C:\USERS\%1" GOTO :EOF

 

:RETRYPATHSECOND

ECHO. Error cleaning profile for: %1 - Trying again.

rmdir C:\USERS\%1 /s /q > NUL

GOTO :EOF

 

:REGISTRY

ECHO.------------

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

 

::The following is where it parses the registry data and checks it against the user path. Copy/paste the IF line and make the user modification needed.

 

:REGCHECK

FOR /f "tokens=3" %%b in ('reg query %1 /v ProfileImagePath') DO SET USERREG=%%b

IF /i [%USERREG%]==[c:\Users\Administrator] GOTO :REGSKIP

GOTO REGCLEAN

 

:REGSKIP

ECHO. Skipping registry clean for %USERREG%

GOTO :EOF

 

:REGCLEAN

ECHO. Cleaning registry for: %USERREG%

reg delete %1 /f

GOTO :EOF

 

::The cleaning portion of the script is now done. Now begins the verification and log reporting.

 

:VERIFY

FOR /f "tokens=*" %%c IN ('dir c:\USERS /b /ad') DO CALL :VERIFYPATH "%%c"

 

::Same thing as the clean - if you need to exclude an account, make your copy/paste below.

 

:VERIFYPATH

IF /i [%1]==["Administrator"] GOTO :EOF

IF /i [%1]==["All Users"] GOTO :EOF

IF /i [%1]==["Default"] GOTO :EOF

IF /i [%1]==["Default user"] GOTO :EOF

IF /i [%1]==["public"] GOTO :EOF

GOTO VERPATHREPORT

 

:VERPATHREPORT

ECHO. %1

IF /i [%1]==[] (

set PATHRESULT=PATH_SUCCESS

) ELSE (

set PATHRESULT=PATH_FAILURE

)

ECHO. %PATHRESULT%

GOTO REGVERIFY

 

:REGVERIFY

ECHO.------------

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

GOTO REGVERIFYECHO

 

::Same thing as the registry clean - copy/paste excluded profiles below.

 

:REGCHECKVERIFY

FOR /f "tokens=3" %%e in ('reg query %1 /v ProfileImagePath') DO SET USERREGV=%%e

IF /i [%USERREGV%]==[c:\Users\Administrator] GOTO :EOF

GOTO REGVERIFYECHO

 

:REGVERIFYECHO

ECHO. %1

IF /i [%1]==[] (

set REGRESULT=REG_SUCCESS

) ELSE (

set REGRESULT=REG_FAILURE

)

ECHO. %REGRESULT%

GOTO REPORTCHECK

 

::The following is where you would enter the mapped drive path.

::You can use a straight UNC if you like, but I find this to be a bit

::more solid and it allows you to use different creds in case you

::automate it for a local scheduled task to run as local admin.

 

:REPORTCHECK

'net use t: \\server\path

 

'IF EXIST "t:\labreport.txt" (

'GOTO REPORTGEN

') ELSE (

'GOTO EXIT

')

 

::This is a time/date stamp creator that I actually pulled from a Minecraft

::to Dropbox backup script I made a long while back.

 

':REPORTGEN

'FOR /F "tokens=1 delims=:" %%f in ('time /T') DO SET T=%%f

'FOR /F "tokens=*" %%g in ('echo %date:~10,4%-%date:~4,2%-%date:~7,2% %T%-%time:~3,2%-%time:~6,2%') DO SET TDATETIME=%%g

 

ECHO. %PATHRESULT% %REGRESULT% %COMPUTERNAME% %TDATETIME% >> "t:\labreport.txt"

net use t: /delete

GOTO EXIT

 

:EXIT

exit

 

:EOF

  • Thanks 1
Posted
This may be of some help to people Its vb script to delete the xp user folder on the computer. You can add exceptions in there too so it doesnt delete certain folders. In our case the students have numbers in from of their usernames so we added exceptions to say if there is a number as the fisrt character we allow it to be delete. This stops it deleting staff profiles as they start with letters.
  • Thanks 1
Posted

Don't use scripts that delete the folder and registry entries, its a dirty hack, this script removes user profiles older than the specified number of days using the Win32_UserProfiles namespace functions, allowing windows to do the work the right way.

 

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 = 14

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

  • Thanks 2

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...