Chev's DevOps Point of View

Chev's DevOps Point of View

How to fetch a user’s profile from Azure Active Directory using PowerShell

On my recent journey helping customers migrate from TFS to VSTS; one of the most common obstacle is verifying that users marked for active import to VSTS have matching AAD records. Thankfully there is a fairly streamlined way to do this using PowerShell and the Azure Graph API.

The magic is in the bellow PowerShell script. Feel free to copy and use it at your convenience.

#Author: Chev Bryan
#Company: Microsoft Corp.
#Date: May 01, 2019 00:00:01 EST

##First time running the script,
##ensure the AzureAD and MSOnline Modules are installed
#Install-Module AzureAD
#Install-Module MSOnline

#Connect to the MSOnline Service
#Please note: You must call the Connect-MsolService cmdlet before calling any other cmdlets.

#Fetch user by Principal Name (email address)
#Be sure to replace the with the email address of the user you wish to fetch
Get-MsolUser -UserPrincipalName


Please note, the first time you run this script, you may have to install the "AzureAD" and "MSOnline" Modules. To do that, simply un-comment the following lines from the code snippet above:

Install-Module AzureAD
Install-Module MSOnline

You can copy and run this script from PowerShell ISE, or save it and run it directly from the PowerShell command line.

Happy Scripting!

Comments (2) -

  • Brian Jackett

    6/11/2019 1:09:45 AM | Reply

    If you require a module that for the script to execute you could handle it in a couple of ways.  Either 1) use a "#requires -Module AzureAD" statement near the top of script or 2) check if the module is installed prior to installing.

    if((Get-InstalledModule AzureAD -ErrorAction SilentlyContinue) -eq $null)
      Install-Module AzureAD

    My personal preference is option 1 as that allows the person running script to install the module in the method that best suits them.

    • Chev

      4/13/2020 9:19:19 AM | Reply

      I agree, there are multiple ways to handle module dependency, and this is a robust one. Thank you for sharing!