Script to delete users profile only months old profile with the Registry itself but should leave current profile intact

profilescriptinguserswindows-registry

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

Best Answer

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?

Edit:

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