How to retrieve information about all user profiles on the system with PowerShell

Configurare noua (How To)

Situatie

  1. Retrieves information about all user profiles on the system.
  2. Filters out system accounts and profiles with no home directory.
  3. Sorts the profiles based on the last logon time.
  4. 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”

Tip solutie

Permanent

Voteaza

(6 din 8 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?