Situatie
- Retrieves information about all user profiles on the system.
- Filters out system accounts and profiles with no home directory.
- Sorts the profiles based on the last logon time.
- Outputs the results to a CSV file.
Solutie
# Get all user profiles
$profiles = Get-WmiObject Win32_UserProfile | Where-Object { $_.Special -eq $false -and $_.LocalPath -ne $null }
# Sort profiles based on last logon time
$sortedProfiles = $profiles | Sort-Object LastUseTime -Descending
# Define the output CSV file path
$outputCsvPath = “C:\UserProfilesReport.csv”
# Create an array to store profile information
$profileInfo = @()
# Iterate through sorted profiles
foreach ($profile in $sortedProfiles) {
$profileData = [PSCustomObject]@{
‘UserName’ = $profile.LocalPath.Split(‘\’)[-1]
‘LastLogon’ = $profile.LastUseTime
‘ProfilePath’ = $profile.LocalPath
‘SID’ = $profile.SID
}
# Add profile data to the array
$profileInfo += $profileData
}
# Export the profile information to a CSV file
$profileInfo | Export-Csv -Path $outputCsvPath -NoTypeInformation
# Display a summary
Write-Host “User Profiles Report:”
Write-Host “Results saved to $outputCsvPath”
Leave A Comment?