+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
Windows 7 Thread, Delete user profiles older than a specified number of days in Technical; I'm looking to use the above policy setting on our Win7 machines to clear old, unused profiles. We use the ...
  1. #1
    Sheridan's Avatar
    Join Date
    Oct 2010
    Posts
    1,161
    Thank Post
    89
    Thanked 84 Times in 56 Posts
    Rep Power
    28

    Delete user profiles older than a specified number of days

    I'm looking to use the above policy setting on our Win7 machines to clear old, unused profiles.

    We use the local Administrator account, and prefer to keep the IT login's as well. Is there any way they can be kept and the above policy used as well?

    Or has anyone managed to script the delete of old profiles (with exceptions) on Windows 7 machines? I know it causes problems as each user has to have their folder deleted and also the registry key as well, which is why this policy setting looks useful.

    I've set most users as 'guests' of the machine but this means no cached logins, which increases the login time for regular users.

  2. #2

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    21
    This script will delete profiles which are older than the specified number of days using WMI (no ugly registry hacking). You can modify the excluded accounts and the maximum age of profiles by modify the strFilter and intMaxProfileAge variables respectively.

    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
    Last edited by ChrisMiles; 6th July 2011 at 01:06 PM.

  3. Thanks to ChrisMiles from:

    Sheridan (6th July 2011)

  4. #3
    Sheridan's Avatar
    Join Date
    Oct 2010
    Posts
    1,161
    Thank Post
    89
    Thanked 84 Times in 56 Posts
    Rep Power
    28
    Does that remove the folder and the registry entries as well? if so that would be perfect!

  5. #4

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    21
    The scripts that remove folders and registry entries are a hack, this script gets windows to do all the work just as if you had done it through the User Profiles interface so yeah, windows will remove everything including the folders and registry entries. I used this myself in a similar form until i wrote a windows app which allows me to remove specific user profiles from any and all machines in addition to this script.

  6. #5

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,216
    Thank Post
    2,767
    Thanked 935 Times in 875 Posts
    Rep Power
    343
    Delprof

    Scripting Microsoft Delprof.exe / Delprof script examples

    You can change the period to 30 instead of 20 in conjunction with user hive profile service

  7. Thanks to mac_shinobi from:

    Sheridan (6th July 2011)

  8. #6
    Sheridan's Avatar
    Join Date
    Oct 2010
    Posts
    1,161
    Thank Post
    89
    Thanked 84 Times in 56 Posts
    Rep Power
    28
    Great, thanks - I'll try those and and see what works best for me

  9. #7

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    21
    Quote Originally Posted by mac_shinobi View Post
    Delprof

    Scripting Microsoft Delprof.exe / Delprof script examples

    You can change the period to 30 instead of 20 in conjunction with user hive profile service
    Microsoft's delprof.exe doesnt support windows 7.

  10. Thanks to ChrisMiles from:

    mac_shinobi (6th July 2011)

  11. #8

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,216
    Thank Post
    2,767
    Thanked 935 Times in 875 Posts
    Rep Power
    343
    Quote Originally Posted by ChrisMiles View Post
    Microsoft's delprof.exe doesnt support windows 7.
    that counts that out then, lol didnt realise it was in the win 7 section, doh

  12. #9
    Sheridan's Avatar
    Join Date
    Oct 2010
    Posts
    1,161
    Thank Post
    89
    Thanked 84 Times in 56 Posts
    Rep Power
    28
    I tried the Win7 script (ChrisMiles) and it seems to be sticking when trying to delete a profile - I get an error 438 at the point of deleting a matching profile?

  13. #10

  14. #11

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    21
    Did you run the script with admin privileges?

  15. #12
    Sheridan's Avatar
    Join Date
    Oct 2010
    Posts
    1,161
    Thank Post
    89
    Thanked 84 Times in 56 Posts
    Rep Power
    28
    I was running it as a logged in Administrator? I guess normally this would be in the Startup Scripts of a GPO

    Edit - ran in an elevated command prompt and get 438 at the point of delete for each profile
    Last edited by Sheridan; 7th July 2011 at 11:49 AM.

  16. #13
    Sheridan's Avatar
    Join Date
    Oct 2010
    Posts
    1,161
    Thank Post
    89
    Thanked 84 Times in 56 Posts
    Rep Power
    28
    <embarrassed> Oops - I've just checked and I've somehow deleted the underscore at the end of 'objprofile.Delete', must have been when I hit return to put a debug Msgbox statement in! </embarrased>

    My fault! Working perfectly now - cheers for the info.

  17. #14
    MattGibson's Avatar
    Join Date
    May 2008
    Location
    Epsom
    Posts
    160
    Thank Post
    16
    Thanked 8 Times in 5 Posts
    Rep Power
    14
    Does this work for Windows 7?

    I've just tested the script and it prompts me with a dialogue box displaying '0'

    Regards
    Matt

  18. #15

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,656
    Thank Post
    324
    Thanked 506 Times in 474 Posts
    Rep Power
    177
    Quote Originally Posted by MattGibson View Post
    Does this work for Windows 7?

    I've just tested the script and it prompts me with a dialogue box displaying '0'

    Regards
    Matt
    If you're running that script above a bit seems
    Code:
    MsgBox DateDiff("d", dtmLastUseTime, Date)
    Is what you mean, could comment it out if it's annoying you? Not sure if it's just left over debugging, or if it's supposed ot be showing though

    Steve

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 18
    Last Post: 13th August 2012, 05:58 PM
  2. Replies: 28
    Last Post: 23rd May 2011, 09:57 AM
  3. Replies: 2
    Last Post: 13th January 2011, 12:28 PM
  4. Delete User profiles on local machines
    By martinb in forum How do you do....it?
    Replies: 2
    Last Post: 16th March 2010, 10:13 AM
  5. Replies: 6
    Last Post: 26th September 2008, 03:57 PM

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
  •