Script to retrieve User Principal Name from Azure Portal

Geo Thomas 1 Reputation point
2022-06-01T02:47:21.63+00:00

Please help me in retrieving the User Principal Name from the Azure AD portal though bat script.

Windows for business Windows Server User experience PowerShell
Microsoft Security Microsoft Entra Microsoft Entra ID
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Newbie Jones 1,386 Reputation points
    2022-06-01T09:27:38.083+00:00

    As there are so many different ways to get this information. To give you an more accurate response, you probably need to provide more context.

    Assuming that you know how to use PowerShell at a basic level. That you know how to connect to Azure AD.

    One way is using the cmdlet Get-AzureADUser.

    To get properties, you have to pipe to a select command

    Get-AzureADUser - ObjectID "username@domain" | Select-Object Department
    

    To get all properties, you also need to use the select command

    Get-AzureADUser -ObjectID "username@domain" | Select *
    

    To specifically get the Display Name and User Principal Name.

        Get-AzureADUser - ObjectID "username@domain" | Select-Object DisplayName, UserPrincipalName
    
    1 person found this answer helpful.

  2. Shashi Shailaj 7,631 Reputation points Microsoft Employee Moderator
    2022-06-01T09:43:06.547+00:00

    @Geo Thomas ,

    As far as i understand you require Userprincipal names of all the users within your Azure AD organization. You can use the following script to get the output . You require Azure AD PowerShell
    Module installed on your machine where you run the following script. Please use the following cmdlet for installing Azure AD PowerShell module.

    Install-Module AzureAD

    In case you require more information on how to install the powershell module , please check the article how to install Azure AD powershell . You would require to update the variable named $tid which is tenant ID for your tenant . You can find the same using Azure portal .

    portal-tenant-id.png

    $tid = 'xxxxx-xxx-xxx-xxx-xxxx'  # Please add your tenant ID  inside single quotes here.   
    Connect-AzureAD -TenantId $tid  
    $allUsers = Get-AzureADUser -All $true  
    $allUsers.Count  
    $userInfo = @()  
    $uid = 0  
    for($x = 0; $x -le $allUsers.Count; $x++)   
    {  
        Write-Progress -Id 0 -Activity "Retrieving User " -Status "$uid of $($allUsers.Count)"   
        $userInfo += ($allUsers)[$x] | Select-Object -Property *  
        $uid++  
    }  
    Write-Progress -Id 0 -Activity " " -Status " " -Completed  
    $userInfo | Select DisplayName, AccountEnabled, UserPrincipalName| Format-Table  
    $userInfo | Select DisplayName, AccountEnabled, UserPrincipalName| Export-Csv ./AADusers.csv -NoTypeInformation -Append  
    

    Once you run the script a file named AADUsers.csv will be created in the working directory which will have an output like below.

    207531-image.png

    You can update the script with the properties you would like to see in addition to userprincipalname in the line 14 and 15 in the above script and this will work for you . Hope this is helpful . Should you need more information please do let us know and we will be happy to help you further on this. In case the information is helpful , please do accept the post as answer which will improve the relevancy of the post and improve discoverability of this thread within the forum for others looking for answer on similar issues .

    Thank you .

    ----------------------------------------------------------------------------------------------------------------------------------------------------------

    • Please don't forget to click on 130616-image.png whenever the information provided helps you. Original posters help the community find answers faster by identifying the correct answer. Here is how
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification
    • If you are interested in joining the VM program and help shape the future of Q&A: Here is how you can be part of Q&A Volunteer Moderators

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.