Forum Discussion

mfranhind115's avatar
mfranhind115
Brass Contributor
Jul 08, 2022

msonline_Get-MsolUser how to get info about DomainName

Hi all,

 

I'm getting info of the users under my tenant, from powershell with Module MSOnline.

 

how can I get the domain name?

 

if I run a command like this:

 

Get-MsolUser | Select-Object UserPrincipalName, DisplayName, licenses, DomainName | Where-Object {($_.licenses).AccountSkuId -match "O365_BUSINESS_ESSENTIALS"}

 

I get DomainName as a NULL value....

 

UserPrincipalName DisplayName Licenses DomainName

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

email address removed for privacy reasons amm {mytenant:ATP_ENTERPRISE, mytenant:O365_BUSINESS_ESSENTIALS}

email address removed for privacy reasons Roberto Surname1 {mytenant:ATP_ENTERPRISE, mytenant:O365_BUSINESS_ESSENTIALS}

email address removed for privacy reasons Michela Surname2 {mytenant:ATP_ENTERPRISE, mytenant:O365_BUSINESS_ESSENTIALS}

email address removed for privacy reasons Daria Surname3 {mytenant:ATP_ENTERPRISE, mytenant:O365_BUSINESS_ESSENTIALS}

email address removed for privacy reasons Giorgio Surname4 {mytenant:ATP_ENTERPRISE, mytenant:O365_BUSINESS_ESSENTIALS}

email address removed for privacy reasons Orietta Surname5 {mytenant:ATP_ENTERPRISE, mytenant:O365_BUSINESS_ESSENTIALS}

...

 

thanks!

  • mfranhind115 

     

    Yes, that makes sense as there is no attribute returned named DomainName.

     

    Get-MsolUser returns an object of type [Microsoft.Online.Administration.User].

     

    If you run the following command, you will see that DomainName is not part of this class, meaning any reference to it would naturally result in a $null return value.

     

    [Microsoft.Online.Administration.User]::new() | Get-Member;

     

    If you're after the Azure domain name, then simply split the userPrincipalName, as the domain component can only be that of a registered domain name.

     

    If you're after the on-premise Active Directory domain name, look at the Get-MgUser command from the Microsoft.Graph.Users module, from which the relevant property is named OnPremisesDomainName.

     

     

    Note that for the OnPremisesDomainName to be populated, you will need to select the "beta" endpoint, which you can do via the following command (after connecting via Connect-MgGraph, of course):

     

    Select-MgProfile -Name beta;

     

    You could always use native REST calls but my guess is you'll find using an existing Microsoft module easier to navigate.

     

    Cheers,

    Lain

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    mfranhind115 

     

    Yes, that makes sense as there is no attribute returned named DomainName.

     

    Get-MsolUser returns an object of type [Microsoft.Online.Administration.User].

     

    If you run the following command, you will see that DomainName is not part of this class, meaning any reference to it would naturally result in a $null return value.

     

    [Microsoft.Online.Administration.User]::new() | Get-Member;

     

    If you're after the Azure domain name, then simply split the userPrincipalName, as the domain component can only be that of a registered domain name.

     

    If you're after the on-premise Active Directory domain name, look at the Get-MgUser command from the Microsoft.Graph.Users module, from which the relevant property is named OnPremisesDomainName.

     

     

    Note that for the OnPremisesDomainName to be populated, you will need to select the "beta" endpoint, which you can do via the following command (after connecting via Connect-MgGraph, of course):

     

    Select-MgProfile -Name beta;

     

    You could always use native REST calls but my guess is you'll find using an existing Microsoft module easier to navigate.

     

    Cheers,

    Lain

Resources