This script will loop through all the folders in the “C:\Users” directory and check if they are a container (i.e. a folder). If they are, it will calculate the last time the user profile folder was accessed and check if it was accessed over 90 days ago. If it was, it will print the name of the user, the last accessed date, and the number of days inactive. You can adjust the value of the $days variable to specify the number of days of inactivity after which the profile will be considered inactive.
Note: This script will not delete the profile it will just find the user profile that not used for a long time.
$days = 90
$usersPath = "C:\Users"
$currentDate = (Get-Date).Date
$users = Get-ChildItem -Path $usersPath
foreach ($user in $users) {
if ($user.PSIsContainer) {
$folderLastAccessed = (Get-ChildItem $user.FullName | Where-Object {$_.PSIsContainer -eq $false} | Sort-Object LastAccessTime -Descending | Select-Object -First 1).LastAccessTime
$daysInactive = ($currentDate - $folderLastAccessed.Date).TotalDays
if ($daysInactive -gt $days) {
Write-Host "User: $($user.Name) - Last Accessed: $folderLastAccessed - Inactive for: $daysInactive days"
}
}
}