Post

AD User Detailed Check PowerShell

# Add user CSV file:
$csvPath = "C:\100.csv"

# Export CSV:
$outputPath = "C:\output.csv"

# Import data:
$users = Import-Csv -Path $csvPath

# Export info:
$output = @()

# For each user:
foreach ($user in $users) {
# SamAccountName found..
if (-not $user.SamAccountName) {
Write-Host "Empty SamAccountName Found!" -ForegroundColor Yellow
continue
}

try {
# GEt user AD:
$adUser = Get-ADUser -Filter "SamAccountName -eq '$($user.SamAccountName)'" -Properties DisplayName, SamAccountName, LastLogonDate, Enabled

if ($adUser) {
# Add user information:
$output += [PSCustomObject]@{
"Ad Soyad" = $adUser.DisplayName
"TCNO" = $adUser.SamAccountName
"Son Logon Date" = if ($adUser.LastLogonDate) { $adUser.LastLogonDate } else { "no login" }
"Hesap Durumu" = if ($adUser.Enabled) { "Enabled" } else { "Disabled" }
}
} else {
# User not found warning:
$output += [PSCustomObject]@{
"TCNO" = "Not found!"
"Ad Soyad" = "Not found!"
"Son Logon Date" = "N/A"
"Hesap Durumu" = "N/A"
}
}
} catch {
Write-Host "An error occurred: $_" -ForegroundColor Red
}
}

# Save file xx.csv
$output | Export-Csv -Path $outputPath -NoTypeInformation -Encoding UTF8

Write-Host "Process completed! Output file: $outputPath"

This post is licensed under CC BY 4.0 by the author.