PowerShell script to find user profile that not used for long time

Powershell script for users list with the folder size and last use
January 23, 2023
Powershell script to find the users folder size and last modified
January 23, 2023

PowerShell script to find user profile that not used for long time

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"
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *