$users = Get-ADUser -Filter {Enabled -eq $true}
# Create an array to hold the user details
$userDetails = @()
# Loop through each user and get first name, last name, and email address
foreach ($user in $users) {
$firstName = $user.GivenName
$lastName = $user.Surname
$name = $user.Name
$UserPrincipalName = $user.UserPrincipalName
$SamAccountName = $user.SamAccountName
# Create a hashtable with the user's details
$userHashtable = @{
FirstName = $firstName
LastName = $lastName
Name = $name
UserPrincipalName = $UserPrincipalName
SamAccountName = $SamAccountName
}
# Add the hashtable to the user details array
$userDetails += New-Object PSObject -Property $userHashtable
}
# Export the user details to a CSV file
$userDetails | Export-Csv -Path C:\temp\user_details.csv -NoTypeInformation