# Export local users who have logged on within the last 6 months $CutoffDate = (Get-Date).AddMonths(-6) # Built-in Windows accounts to exclude $ExcludedUsers = @( "Administrator", "Guest", "Gast", "DefaultAccount", "WDAGUtilityAccount", "defaultuser0" ) $Results = foreach ($Profile in Get-CimInstance Win32_UserProfile) { # Skip system profiles if ($Profile.Special) { continue } # Only process profiles located under C:\Users if ($Profile.LocalPath -notlike "C:\Users\*") { continue } # Only include profiles used within the last 6 months if ($Profile.LastUseTime -lt $CutoffDate) { continue } $UserName = Split-Path $Profile.LocalPath -Leaf if ($UserName -in $ExcludedUsers) { continue } $User = Get-LocalUser -Name $UserName -ErrorAction SilentlyContinue if ($null -eq $User) { continue } # Use the account name if FullName is empty if ([string]::IsNullOrWhiteSpace($User.FullName)) { $FullName = $User.Name } else { $FullName = $User.FullName } [PSCustomObject]@{ Name = $User.Name FullName = $FullName Password = "StartPassword123!" LastUseTime = $Profile.LastUseTime } } $Results | Sort-Object Name | Export-Csv "C:\Temp\Users.csv" -NoTypeInformation -Encoding UTF8 Write-Host "" Write-Host "$($Results.Count) users exported." Write-Host "Output file: C:\Temp\Users.csv"