Sheridan Posted July 6, 2011 Posted July 6, 2011 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.
ChrisMiles Posted July 6, 2011 Posted July 6, 2011 (edited) 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. 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 Edited July 6, 2011 by ChrisMiles 1
Sheridan Posted July 6, 2011 Author Posted July 6, 2011 Does that remove the folder and the registry entries as well? if so that would be perfect!
ChrisMiles Posted July 6, 2011 Posted July 6, 2011 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.
mac_shinobi Posted July 6, 2011 Posted July 6, 2011 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 1
Sheridan Posted July 6, 2011 Author Posted July 6, 2011 Great, thanks - I'll try those and and see what works best for me
ChrisMiles Posted July 6, 2011 Posted July 6, 2011 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. 1
mac_shinobi Posted July 6, 2011 Posted July 6, 2011 Microsoft's delprof.exe doesnt support windows 7. that counts that out then, lol didnt realise it was in the win 7 section, doh
Sheridan Posted July 7, 2011 Author Posted July 7, 2011 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?
Guest TheLibrarian Posted July 7, 2011 Posted July 7, 2011 Try this: Delprof2 – User Profile Deletion Tool | Helge Klein | Home of SetACL
Sheridan Posted July 7, 2011 Author Posted July 7, 2011 (edited) 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 Edited July 7, 2011 by Sheridan
Sheridan Posted July 7, 2011 Author Posted July 7, 2011 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! My fault! Working perfectly now - cheers for the info.
MattGibson Posted March 7, 2012 Posted March 7, 2012 Does this work for Windows 7? I've just tested the script and it prompts me with a dialogue box displaying '0' Regards Matt
Steve21 Posted March 7, 2012 Posted March 7, 2012 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 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
MattGibson Posted March 7, 2012 Posted March 7, 2012 HI Steve, Thanks for the quick reply! I've taken it out but now getting other errors I'm guessing I'm not doing something quite right....
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now