Forum Discussion
John Andrews
Nov 17, 2021Copper Contributor
Windows 10 Delete User Profiles Older Than 60 Days
Hi. I am working on trying to automate cleaning up local user profiles on some machines that after awhile run into HDD space issues. I've been working on a script below for this, but I cannot get it ...
IggyG10
Nov 04, 2022Copper Contributor
Very late response, but I am actually working on this same issue. This is what worked for me.
I removed
-and $_.Name -ne "Public" -and $_.Name -ne "defaultuser0" -and $_.LocalPath -like "C:\Users\*"}
because
| where {$_.Loaded -eq $false} |
will replace this in my case.
Finds and selects your users
Get-CimInstance -ClassName Win32_UserProfile | Where {$_.CreationTime -lt (get-date).adddays(-60)} | where {$_.Loaded -eq $false} | select -Property LocalPath,Loaded,PSComputerName | ft -AutoSize
Deletes your users
Get-CimInstance -ClassName Win32_UserProfile | Where {$_.CreationTime -lt (get-date).adddays(-60)} | where {$_.Loaded -eq $false} | Remove-CimInstance -Verbose -Confirm:$false
Not very pretty but it gets the job done.
peddy396
Feb 02, 2023Copper Contributor
$currentDate = Get-Date
$oneYearAgo = $currentDate.AddYears(-1)
Get-WmiObject -Class Win32_UserProfile | ForEach-Object {
if ($_.ConvertToDateTime($_.LastUseTime) -lt $oneYearAgo) {
Remove-WmiObject -InputObject $_ -Confirm:$false
}
}
Deploy as a startup script. This works very well