usrclass.dat user profile clean up
$ErrorActionPreference = "SilentlyContinue"
$Report = $Null
$Path = "C:\Users"
$ExcludedUsers ="Default", "Public", "Administrator","*$*",".NET *","*MS*","Default."
$UserFolders = $Path | GCI -Directory -Exclude $ExcludedUsers
ForEach ($UserFolder in $UserFolders)
{
$UserName = $UserFolder.Name
$Db = Get-Item "$Path\$Username\AppData\Local\Microsoft\Windows\UsrClass.dat" -force
If($db){
$UserClass = $Db.LastWriteTime
if ($($UserClass.Year) -lt ((Get-Date).AddYears(-1)).year)
{
Write-Host "Going to be delete the user folder $UserName and its last used by usrclass.dat date is : $UserClass " -ForegroundColor Cyan
Set-Location C:\Users
Remove-Item -Path $($username) -Recurse -Force -WhatIf
$profilePath = "C:\Users\$UserName"
$profileListKeys = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
foreach ($key in $profileListKeys) {
$profileImagePath = Get-ItemProperty -Path $key.PSPath | Select-Object -ExpandProperty ProfileImagePath
if ($profileImagePath -eq $profilePath) {
Write-Host "Deleting registry key: $($key.Name) - $username"
Remove-Item -Path $key.PSPath -Force -WhatIf
}
}
if(Test-Path $UserName){cmd.exe /c rd /s /q "C:\Users\$UserName"}
}
else {
Write-Host "Not Going to be delete the user folder $UserName because last used by usrclass.dat date is : $UserClass "
}
}
}
Comments
Post a Comment