$days = 90
$usersPath = "C:\Users"
$outputFile = "C:\Users_Inactive_Over_90_Days.csv"
$currentDate = (Get-Date).Date
$users = Get-ChildItem -Path $usersPath
$results = @()
foreach ($user in $users) {
if ($user.PSIsContainer) {
$folderSize = (Get-ChildItem $user.FullName -Recurse | Measure-Object -Property Length -Sum).Sum
$folderLastModified = (Get-ChildItem $user.FullName -Recurse | Sort-Object LastWriteTime -Descending | Select-Object -First 1).LastWriteTime
$daysInactive = ($currentDate - $folderLastModified.Date).TotalDays
if ($daysInactive -gt $days) {
$results += New-Object PSObject -Property @{
"User" = $user.Name
"FolderSize" = "{0:N2}" -f ($folderSize/1MB)
"LastModified" = $folderLastModified
"DaysInactive" = $daysInactive
}
}
}
}
$results | Export-Csv $outputFile -NoTypeInformation
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 size of the folder (including all subfolders and files) using the Measure-Object cmdlet, the last time the folder was modified using the Sort-Object cmdlet, and the number of days since the folder was last modified. If the folder has been modified over 90 days ago, it will add the user name, folder size, last modified date, and number of days inactive to an array.
After the loop, it will export the array to a csv file in the specified location. This way you can have a report of all the inactive user profiles and review them later.
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 and the outputFile location to export the file.