We had a client with over 100 user profile folders in C:\USERS, so they wanted to know which ones are still being used and which ones are not.
The first thing we did is to click on the START button and typed Advanced System Settings
. Then we clicked the SETTINGS button in the USER PROFILES section and deleted a dozen accounts listed as UNKNOWN. Unknown means Windows cannot match the users GUID to a user name, and that happens when the user no-longer exists.
That left more than 80 user profiles to interogate and so we wrote the PowerShell script below. The “Last Used” information is taken from the registry and not the folder modified date. It uses the Win32_UserProfile
WMI class to pull the LastUseTime (REG_QWORD)
from HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
As you can see in the screenshot above it outputs the list to the screen, but if you look at the top of script you can see it will also output to C:\temp\users.csv so you can make a pretty report in Excel.
# Define the output file path
$outputPath = "C:\temp\users.csv"
# Create the output directory if it doesn't exist
$outputDir = Split-Path -Path $outputPath -Parent
if (-not (Test-Path -Path $outputDir -PathType Container)) {
try {
New-Item -Path $outputDir -ItemType Directory -Force | Out-Null
} catch {
Write-Error "Failed to create directory $($_.Exception.Message)"
exit
}
}
# Get all subdirectories in C:\Users\
$userFolders = Get-ChildItem -Path "C:\Users\" -Directory
# Get user profiles from WMI and create a lookup table
$wmiProfileInfo = @{}
Get-WmiObject -Class Win32_UserProfile | ForEach-Object {
$wmiProfileInfo[$_.LocalPath.ToLower()] = $_
}
# Collect profile information
$allProfiles = foreach ($folder in $userFolders) {
$localPathLower = $folder.FullName.ToLower()
$wmiData = $wmiProfileInfo[$localPathLower]
$lastUsed = if ($wmiData.LastUseTime) {
try {
(Get-Date ([System.Management.ManagementDateTimeConverter]::ToDateTime($wmiData.LastUseTime)) -Format "d")
} catch {
"Invalid Date"
}
} else {
try {
(Get-Date (Get-Item -Path $folder.FullName).LastWriteTime -Format "d")
} catch {
"N/A"
}
}
[PSCustomObject]@{
Username = $folder.Name
LastUsed = $lastUsed
Loaded = $wmiData.Loaded
IsRoaming = ($wmiData.RoamingPath -ne $null -and $wmiData.RoamingPath -ne "")
LocalPath = $folder.FullName
}
}
# Output to the screen
Write-Host "User and System Profile Information (from C:\Users\):"
$allProfiles | Format-Table
# Output to CSV file
try {
$allProfiles | Export-Csv -Path $outputPath -NoTypeInformation
Write-Host "`nUser and system profile information exported to: $outputPath"
} catch {
Write-Error "Failed to export to CSV: $($_.Exception.Message)"
}
0 Comments