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

Prince Chauhan 0 Reputation points
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 Identity Manager
Microsoft Identity Manager
A family of Microsoft products that manage a user's digital identity using identity synchronization, certificate management, and user provisioning.
706 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,065 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Navya 11,710 Reputation points Microsoft Vendor
    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.