Thank you for posting this in Microsoft Q&A.
You can use below PowerShell script.
# Connect to Azure AD
Connect-AzureAD
# Get all Azure AD users
$users = Get-AzureADUser -All $true
# Create an empty array to store user data
$userData = @()
# Loop through each user
foreach ($user in $users) {
# Get the last login date
$signInActivity = Get-AzureADAuditSignInLogs -Top 1 -Filter "userPrincipalName eq '$($user.UserPrincipalName)'" | Sort-Object -Property CreatedDateTime -Descending
$lastLoginDate = $signInActivity.CreatedDateTime
# Get the manager
$manager = Get-AzureADUserManager -ObjectId $user.ObjectId
$managerName = if ($manager) { $manager.DisplayName } else { "No Manager" }
# Add user data to the array
$userData += [PSCustomObject]@{
UserPrincipalName = $user.UserPrincipalName
DisplayName = $user.DisplayName
LastLoginDate = $lastLoginDate
CompanyName = $user.CompanyName
ManagerName = $managerName
}
}
# Export user data to CSV
$userData | Export-Csv -Path "C:\Users\<UserName>\Desktop\AzureADUsers-$(Get-Date -format "MM-dd-yyyy").csv" -NoTypeInformation
Hope this helps. Do let us know if you any further queries.
Thanks,
Navya.
If this answers your query, do click Accept Answer
and Yes
for was this answer helpful. And, if you have any further query do let us know.