Jump to content

Recommended Posts

Posted

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.

Posted (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 by ChrisMiles
  • Thanks 1
Posted
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.
Posted
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?
Posted (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 by Sheridan
Posted

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.

  • 7 months later...
Posted
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...