#PowerShell Script to Remove Old User Profiles, #Deleting Outdated User Profiles with PowerShell, #How to Clean Up User Profiles Older Than One Year Using PowerShell, #PowerShell Method for Deleting Aged User Profiles, #Scripted Cleanup: Remove Older User Profiles with PowerShell
This PowerShell script scans Windows user profiles and removes any that are older than one year. It helps reclaim disk space, enhance system performance, and streamline workstation maintenance by clearing out stale or unused profiles. The script also generates a detailed report in C:\Temp\SystemName_Date_UserProfile.csv for review. Be sure to test it in your own environment before deployment—although it has been successfully tested and is working in ours.
# Define the date 1 year ago from today
$oneYearAgo = (Get-Date).AddMonths(-12)
#Create a log for user profile deletions
$FileSave="C:\temp\$(gc env:computername)_" + (get-date -format "MM-d-yy-HH-mm")+ "_userprofiledeletion.csv"
If(!(test-path -path $FileSave))
{
New-Item -ItemType file -Path $FileSave -Force
}
# Profiles to exclude
$profilesToExclude = @("KKG","specificProfileName")
# Get user profile directories under C:\Users
$UserFolders = Get-ChildItem -Path "C:\Users" -Force -Directory
# Initialize an array to store profile details
$profilesToExport = @()
# Iterate through each user profile directory
foreach ($Folder in $UserFolders) {
# Skip profiles that are in the exclusion list
if ($profilesToExclude -contains $Folder.Name) {
continue
}
# Path to IconCache.db
$iconCachePath = [System.IO.Path]::Combine($Folder.FullName, "AppData\Local\IconCache.db")
Write-Host $iconCachePath
# Initialize status
#$status = "not eligible for deletion"
# Get the LastWriteTime of the profile directory itself
$profileLastWriteTime = $Folder.LastWriteTime
# Calculate the size of the profile directory in GB
[UInt64] $FolderSize = (Get-ChildItem -Path $Folder.FullName -Force -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
$profileSizeGB = [math]::Round($FolderSize / 1GB, 4) # Rounds to 4 decimal places
# Check if IconCache.db exists and get its last write time
if (Test-Path -Path $iconCachePath) {
$file = Get-Item -Path $iconCachePath -Force
$iconCacheLastWriteTime = $file.LastWriteTime
Write-Host $iconCacheLastWriteTime
# Determine if IconCache.db is older than one year
if ($iconCacheLastWriteTime -lt $oneYearAgo) {
# Delete the profile directory
try {
Remove-Item -Path $Folder.FullName -Recurse -Force
$status = "Profile deleted"
} catch {
$status = "failed to delete"
} }
else {
$status = "Not eligible for deletion"
}
} else {
$iconCacheLastWriteTime = "NA"
# Determine if User profile is older than two years
if ($profileLastWriteTime -lt $oneYearAgo)
{
#Delete the user profile directory
try {
Remove-Item -Path $Folder.FullName -Recurse -Force
$status = "Deleting profile considering user profile timestamp >1 Years."
} catch {
$status= "Failed to delete profile."
}
}
else {
$status = "Iconcache.db not found and user profile time < 1 Years. not eligible for deletion"
}
}
# Create a custom object with profile details
$profileDetails = [PSCustomObject]@{
"User Profile" = $Folder.FullName
"Last Write Time of User profile" = $profilelastwritetime
"Last Write Time of IconCache file" = $iconCacheLastWriteTime
"Profile Size (GB)" = $profileSizeGB
"Status" = $status
}
# Add profile details to the array
$profilesToExport += $profileDetails
}
# Export the profile details to a CSV file
#$csvFilePath = "C:\temp\profiles.csv"
$profilesToExport | Export-Csv -Path $Filesave -NoTypeInformation
Write-Output "Exported profile details with size and status to: $Filesave"
No comments:
Post a Comment