first of all sry if this post is repetitive for the profile deletion.

If you have a correct link please post it below or the script itself.

Question: as i have a lot of computers with windows 7 (2400 systems) as the operating system on them and as the users keep on changing or shifting place from one PC to another PC and managing the system or the C drive for the consistent system performance or freeing space is a challenging task.

I would like have a Script that would only delete old (No of Months) profile as well as the registry itself but should keep the current profile instanct the the registry too.

I did find the script on some forum that gets the job done but not the right way as it deletes all the profile and excludes the admin,administrator etc in the script but i cannot delete the old profile of a certain time or date period.

As again sorry if this post looks like mess but help is appreciated


If your workstations are domain joined and you have access to modify group policies then all of this can be specified in GPO;

Computer Configuration | Policies | Administrative Templates | System | User Profiles

Alternatively you should look at delprof2 (https://helgeklein.com/free-tools/delprof2-user-profile-deletion-tool/), this will do what you need.

It's a command line tool (so you can script it) and takes a parameter to specify how old a profile must be before it's eligible for deletion. For example this command will delete all local profiles older than 60 days;

delprof2 /d 60

It has more uses too, such as only deleting profiles that are local caches of roaming profiles (ignoring true local profiles), deleting profiles remotely (so you could potentially script this to run from a server and target each of your workstations in turn), pattern matching for inclusion / exclusion so you can tell it to ignore specific profiles (e.g. all accounts starting with john*).

It's also free in many cases (I quote)

"Delprof2 is free for private use, for educational and non-profit organizations. All other organizations may use Delprof2 for free, too, if they allow us to publish their name. Otherwise a commercial license must be purchased."

Does that help?


Seeing as you appear to want an additional option, try this script or a modification of it. I found it on this forum (http://www.edugeek.net/forums/windows-7/79028-delete-user-profiles-older-than-specified-number-days.html) and it is entirely untested; use at your own risk. You'll probably want to remove the MsgBox line, and the unused period can be adjusted by changing the value of variable 'intMaxProfileAge'

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


            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
                'Profile Deleted.
            End If
        End If
End If
  • yes i did that as well but that is not a proper solution that i am looking out for – Samuel Fernandes Oct 11 '14 at 13:50
  • @SamuelFernandes - I'm sorry, but how is that in any way not a "proper solution"? In any case, I've now provided you with a third option using WMI in a vbscript. Untested, use at your own risk. – Steve365 Oct 11 '14 at 14:23
  • Thanks for that script though it is a complex one. – Samuel Fernandes Dec 22 '14 at 13:07
  • @SamuelFernandes - I hope it works for you. I don't think it's particularly complicated given your requirements. If you feel I've answered your question sufficiently please could you mark the question answered? If you'd like any additional help with the script please do ask. – Steve365 Dec 22 '14 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.