usrclass.dat and IconCache.db 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

$dat = Get-Item "$Path\$Username\AppData\Local\Microsoft\Windows\UsrClass.dat" -force

        If($dat){

$UserClass = $dat.LastWriteTime


$Db = Get-Item "$Path\$Username\AppData\Local\IconCache.db" -force

        If($db){

$IconCache = $Db.LastWriteTime



if( ($($UserClass.Year) -lt ((Get-Date).AddYears(-1)).year) -and ($($IconCache.Year) -lt ((Get-Date).AddYears(-1)).year))

    {

   

    Write-Host "UMCT : Deleting user folder $UserName and its last used by usrclass.dat and IconCache.db date is : $UserClass - $IconCache " -ForegroundColor Cyan

    Set-Location C:\Users

    Remove-Item -Path $($username) -Recurse -Force 

    

                    $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 "UMCT : Deleting registry key: $($key.Name) - $UserName"

                    Remove-Item -Path $key.PSPath -Force

                                                             }

                                                    }

    Set-Location C:\Users

    if(Test-Path $UserName){cmd.exe /c rd /s /q "C:\Users\$UserName"}

    }


else {

   Write-Host "UMCT : Not Deleting user folder $UserName usrclass.dat and IconCache.db is not less than one year old date: $UserClass - $IconCache"

     }


                }

}


}

Comments

Popular posts from this blog

powershell script to export applications and its requirement in weird way

get-allrecent udpated logs

TASK sequence duration report