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?
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?
covered on other eg threads:
Script to delete profile at log off?
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
Last edited by mac_shinobi; 25th January 2012 at 02:21 PM.
speckytecky (25th January 2012)
Thanks mac_shinobi - looks like I didn't do my searches well enough.

mac_shinobi (9th February 2012), speckytecky (26th January 2012)
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.
speckytecky (27th January 2012)
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.
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
speckytecky (9th February 2012)
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.
speckytecky (9th February 2012)
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.
Code: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
link470 (15th March 2012), mac_shinobi (9th February 2012)
There are currently 1 users browsing this thread. (0 members and 1 guests)