Obviously there’s a GPO for this…

…but it’s a bit rubbish and the computer needs a restart for it to ever work and I also didn’t want to wipe out Staff Local User Profiles, I only cared about getting rid of Old Student User Profiles, I do this to ensure there’s enough disk space on the machines, luckily student usernames are all numbers so this was easy to do.

Get-WMIObject -Class Win32_UserProfile |
    Where-Object {(!$_.Special) -and 
                  ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-3)) -and
                  ($_.LocalPath.Split('\')[-1] -match '^\d+$')
    } | Remove-WmiObject

By using PowerShell, we can query WMI and delete old profiles on the machine. The following bit of PowerShell above will ensure special user profiles are not deleted and only delete profiles that are older than 3 days old, obviously you can change this whatever way you see fit. I also do a match so that if there characters in the name and if so the profiles are not deleted as I do not want to get rid of Staff local user profiles.

You can automate this however you like but I personally achieve this by doing a scheduled task GPO that runs a PowerShell script at start up and before the end of the working day.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.