Public/Get-PGStaleProfile.ps1

<#
.SYNOPSIS
Finds local Windows profiles that satisfy the configured stale-profile policy.
 
.DESCRIPTION
Evaluates Windows profile-service activity timestamps, protected-profile rules,
loaded state and profile age. Eligible results are returned oldest first.
 
.PARAMETER MinimumAgeDays
The minimum number of inactive days required for eligibility.
 
.PARAMETER Drive
The local drive used when Windows does not provide a profile-root setting.
 
.PARAMETER ProfileRoot
The directory whose immediate child directories contain local user profiles. The
default is the Windows ProfilesDirectory setting.
 
.PARAMETER ProtectedProfileName
Additional profile directory names that must never be eligible.
 
.PARAMETER IncludeAll
Returns ineligible profiles together with their exclusion reasons.
 
.EXAMPLE
Get-PGStaleProfile -MinimumAgeDays 30
 
.EXAMPLE
Get-PGStaleProfile -IncludeAll | Format-Table UserName, AgeDays, Eligible, ExclusionReason
 
.OUTPUTS
ProfileGuardian.ProfileAssessment
#>

function Get-PGStaleProfile {
    [CmdletBinding()]
    param(
        [ValidateRange(1, 3650)]
        [int]$MinimumAgeDays = 10,

        [string]$Drive,

        [string]$ProfileRoot,

        [string[]]$ProtectedProfileName = @(),

        [switch]$IncludeAll
    )

    Assert-PGWindows
    $resolvedProfileRoot = Resolve-PGProfileRoot -Drive $Drive -ProfileRoot $ProfileRoot
    $cutoff = (Get-Date).AddDays(-$MinimumAgeDays)
    $results = @()

    $profiles = Get-CimInstance -ClassName Win32_UserProfile -ErrorAction Stop
    foreach ($userProfile in $profiles) {
        $reason = Test-PGProfileBaseEligibility -UserProfile $userProfile -ProfileRoot $resolvedProfileRoot -ProtectedProfileName $ProtectedProfileName
        $activity = $null

        if ($null -eq $reason) {
            $activity = Get-PGProfileActivity `
                -Sid ([string]$userProfile.SID) `
                -LocalPath ([string]$userProfile.LocalPath) `
                -LastUseTime $userProfile.LastUseTime
            if ($null -eq $activity) {
                $reason = 'NoReliableActivityTimestamp'
            }
            elseif ($activity.Timestamp -ge $cutoff) {
                $reason = 'NotStale'
            }
        }

        $eligible = $null -eq $reason
        if ($eligible -or $IncludeAll) {
            $lastActivity = if ($null -ne $activity) { $activity.Timestamp } else { $null }
            $ageDays = if ($null -ne $lastActivity) {
                [math]::Floor(((Get-Date) - $lastActivity).TotalDays)
            }
            else {
                $null
            }

            $result = [pscustomobject]@{
                UserName       = if ($userProfile.LocalPath) { Split-Path -Path ([string]$userProfile.LocalPath) -Leaf } else { $null }
                Sid            = [string]$userProfile.SID
                LocalPath      = [string]$userProfile.LocalPath
                LastActivity   = $lastActivity
                AgeDays        = $ageDays
                AgeSource      = if ($null -ne $activity) { $activity.Source } else { $null }
                Loaded         = [bool]$userProfile.Loaded
                Special        = [bool]$userProfile.Special
                Eligible       = $eligible
                ExclusionReason = $reason
            }
            $result.PSObject.TypeNames.Insert(0, 'ProfileGuardian.ProfileAssessment')
            $results += $result
        }
    }

    return $results | Sort-Object -Property @{ Expression = 'LastActivity'; Ascending = $true }, Sid
}