+ Post New Thread
Results 1 to 10 of 10
Windows 7 Thread, Is there a script or GPO setting to clean out local profiles? in Technical; We are having an issue with some of our Windows 7 64 bit laptops failing to pick up network drives ...
  1. #1
    speckytecky's Avatar
    Join Date
    May 2006
    Location
    UK
    Posts
    2,404
    Thank Post
    3,224
    Thanked 245 Times in 198 Posts
    Blog Entries
    3
    Rep Power
    107

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

    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?

  2. #2

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,228
    Thank Post
    2,773
    Thanked 937 Times in 877 Posts
    Rep Power
    343
    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 01:21 PM.

  3. Thanks to mac_shinobi from:

    speckytecky (25th January 2012)

  4. #3
    speckytecky's Avatar
    Join Date
    May 2006
    Location
    UK
    Posts
    2,404
    Thank Post
    3,224
    Thanked 245 Times in 198 Posts
    Blog Entries
    3
    Rep Power
    107
    Thanks mac_shinobi - looks like I didn't do my searches well enough.

  5. #4


    Join Date
    Feb 2007
    Location
    51.405546, -0.510212
    Posts
    8,134
    Thank Post
    204
    Thanked 2,395 Times in 1,773 Posts
    Rep Power
    705
    Quote Originally Posted by mac_shinobi View Post
    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.

  6. 2 Thanks to Arthur:

    mac_shinobi (9th February 2012), speckytecky (26th January 2012)

  7. #5

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,228
    Thank Post
    2,773
    Thanked 937 Times in 877 Posts
    Rep Power
    343
    Quote Originally Posted by Arthur View Post
    The "User Profile Service" built into Windows Vista and 7 makes the User Profile Hive Cleanup Service for XP redundant.
    That answers that question and handy to know, thanks Arthur

  8. #6

    Join Date
    Jan 2012
    Posts
    3
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0
    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.

  9. Thanks to aboell from:

    speckytecky (27th January 2012)

  10. #7
    rich_tech's Avatar
    Join Date
    Mar 2011
    Location
    Wales
    Posts
    924
    Thank Post
    114
    Thanked 116 Times in 100 Posts
    Rep Power
    60
    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.

  11. #8
    adamchapman's Avatar
    Join Date
    Aug 2007
    Location
    North Lincolnshire
    Posts
    187
    Thank Post
    29
    Thanked 14 Times in 14 Posts
    Rep Power
    16
    Quote Originally Posted by speckytecky View Post
    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

  12. Thanks to adamchapman from:

    speckytecky (9th February 2012)

  13. #9

    Join Date
    Feb 2011
    Posts
    77
    Thank Post
    6
    Thanked 22 Times in 13 Posts
    Rep Power
    10
    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.

  14. Thanks to Chunks_ from:

    speckytecky (9th February 2012)

  15. #10

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    21
    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

  16. 2 Thanks to ChrisMiles:

    link470 (15th March 2012), mac_shinobi (9th February 2012)

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 4
    Last Post: 7th December 2011, 12:01 AM
  2. Migrate teachers to new laptops (local profiles) (w7x64 -> w7x64)
    By RabbieBurns in forum How do you do....it?
    Replies: 3
    Last Post: 21st August 2011, 01:50 PM
  3. Replies: 7
    Last Post: 31st March 2009, 07:53 AM
  4. Replies: 5
    Last Post: 25th January 2007, 11:22 PM
  5. Replies: 7
    Last Post: 27th November 2006, 11:26 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •