Azure AD - How to get the AzureAD user name from Azure Active Directory?

SujanChristo 6 Reputation points
2023-02-22T12:19:02.01+00:00

Hi Team,

We are on Microsoft 365 and using AzureAD for authentication in our Windows 10 / 11 machines. We are in need of getting the AzureAD/Username either from Azure Active Directory using Powershell or C# for all the members in the AzureAD environment.

We are searching for this for quite sometime but in vain.

Any help on this would be of greatly appreciated!

Thanks in advance,

With Warm Regards,

Sujan

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,364 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,403 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,802 questions
{count} vote

4 answers

Sort by: Most helpful
  1. Andy David - MVP 142.7K Reputation points MVP
    2023-02-23T12:35:54.8566667+00:00

    ah, ok, if not syncing then you wont be able to get that value from on-prem, short of maybe populating it yourself manually with a script or something and using a custom attribute in Azure perhaps.

    https://learn.microsoft.com/en-us/azure/active-directory/external-identities/user-flow-add-custom-attributes

    1 person found this answer helpful.

  2. Andy David - MVP 142.7K Reputation points MVP
    2023-02-22T15:39:56.8466667+00:00

    Hi there the field is OnPremisesSamAccountName

    so if you were to:

    get-mguser -UserId first.last@contoso.com -Property OnPremisesSamAccountName | select OnPremisesSamAccountName
    

    you will see it


  3. Sujan Christo 1 Reputation point
    2023-03-01T08:00:24.62+00:00

    @Andy David - MVP . thanks a lot for your time and clarifications. That make things clear for us :)

    0 comments No comments

  4. Sujan Christo 1 Reputation point
    2023-03-01T08:05:30.9333333+00:00

    Hi @Andy David - MVP , Did a quick try with both PowerShell and GraphExplorer (https://graph.microsoft.com/v1.0/users/{userObjectId}?$select=onPremisesSamAccountName) and the value returned is null.

    Please note that we are NOT using On-premises Active Directory and Azure AD Connect for Syncing. The machines are directly authenticated with Azure Active Directory.

    Sorry, missed to let you know this scenario earlier :)

    Under this current setup, will we be able to get the onPremisesSamAccountName from AzureAD?

    Kindly share your thoughts on this.

    Thanks a bunch for your time.

    WIth Warm Regards,

    Sujan