Exporting AAD users with specific information via powershell

Comp_tech 20 Reputation points
2023-04-26T10:17:44.07+00:00

Hi, I am trying to export AAD users with below information but I can't get the script working and i think I am missing something in somewhere. Can someone please help? Information I need to include in CSV; id, upn, display name, Group, license name, Company, extentionattribute9, extentionattribute10, extentionattribute11, extentionattribute12 Thanks

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,682 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,498 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JamesTran-MSFT 36,661 Reputation points Microsoft Employee
    2023-04-27T21:04:55.66+00:00

    @Comp_tech

    Thank you for your post and I apologize for the delayed response!

    From your issue, I understand that you're trying to export/download a list of users from Azure AD but you're running into issues with the script not working. To hopefully point you in the right direction, you should be able to reference the following PowerShell script in order to download all your Azure AD users to include your required properties.

    #Connect to Azure AD
    #For more info - https://learn.microsoft.com/en-us/powershell/azure/active-directory/install-adv2?view=azureadps-2.0#installing-the-azure-ad-module
    
    #Install-Module AzureAD
    Connect-AzureAD
    
    
    #Path sets the Output location of the CSV file.
    param(
        [string] $path = "C:\Users\<userName>\Desktop\ADUsers-$(Get-Date -format "MM-dd-yyyy").csv"
    )
    
    #For Each will get all Enabled Azure AD Users and the following properties:
    #Employee ID, First Name, Last Name, Work Email, Job Title, Department, Management Email, License
    & {
        foreach($azuser in Get-AzureADUser -All $true -Filter 'accountEnabled eq true') {
            [pscustomobject]@{
                "Employee ID"   = $azuser.ExtensionProperty["employeeId"]
                "First Name"    = $azuser.givenName
                "Last Name"     = $azuser.surname
                "Work Email"    = $azuser.UserPrincipalName
                "Job Title"     = $azuser.JobTitle
                "Department"    = $azuser.CompanyName
                "Manager Email" = (Get-AzureADUserManager -ObjectId $azuser.ObjectId).UserPrincipalName
                "License"       = $azuser.ExtensionProperty["extension_a92a_msDS_cloudExtensionAttribute1"]
            }
        }
    } | Export-CSV -Path $path -NoTypeInformation
    
    

    If you'd like to change the user properties that're downloaded, you can run the following to get Azure AD user properties.

    #Get a list of 5 Azure AD Users
    Get-AzureADUser -Top 5
    
    #From the list of 5 users - Get a single Azure AD User by Object ID
    Get-AzureADUser -ObjectId "<ObjectID>" | Format-List
    
    
    #Add the properties needed to the PowerShell script, for example you can add UserType to the list of properties downloaded.
    
                "Employee ID"   = $azuser.ExtensionProperty["employeeId"]
                "First Name"    = $azuser.givenName
                "Last Name"     = $azuser.surname
                "Work Email"    = $azuser.UserPrincipalName
                "Job Title"     = $azuser.JobTitle
                "Department"    = $azuser.CompanyName
                "UserType"      = $azuser.UserType
    

    I hope this helps!

    If you have any other questions, please let me know. Thank you for your time and patience throughout this issue.


    If the information helped address your question, please Accept the answer. This will help us and also improve searchability for others in the community who might be researching similar information.


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.