How to Export User manager and Employee Org data along with the below script.

Anonymous
2024-08-20T13:33:27.73+00:00

We are using the below script to export the user information with his/her last login details and we able to fetch it successfully but we are looking some additional details to fetch like Employee Org Data, Manager. Please suggest what should we add here to export these details.

#Connect to Azure AD
Connect-AzureAD

# If you want to test this script on one user
# $users = Get-AzureADUser -ObjectId "<ObjectID>"

# Get all Azure AD users
$users = Get-AzureADUser -All $true


# Create an empty array to store user data
$userData = @()

# Loop through each user and get their last login date
foreach ($user in $users) {
    $signInActivity = Get-AzureADAuditSignInLogs -Top 1 -Filter "userPrincipalName eq '$($user.UserPrincipalName)'" | Sort-Object -Property CreatedDateTime -Descending
    $lastLoginDate = $signInActivity.CreatedDateTime
    $userData += [PSCustomObject]@{
        UserPrincipalName = $user.UserPrincipalName
        DisplayName = $user.DisplayName
        LastLoginDate = $lastLoginDate
    }
}

# Export user data to CSV
$userData | Export-Csv -Path "C:\Users\<UserName>\Desktop\AzureADUsers-$(Get-Date -format "MM-dd-yyyy").csv" -NoTypeInformation


Microsoft Security Microsoft Entra Microsoft Entra ID
Microsoft Security Microsoft Identity Manager
{count} votes

1 answer

Sort by: Most helpful
  1. Navya 19,795 Reputation points Microsoft External Staff Moderator
    2024-08-22T02:47:39.46+00:00

    Hi @Prince Chauhan

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.