Getting user's country in PowerShell

Christine VACHER 21 Reputation points
2022-10-20T17:49:55.263+00:00

I am able to get someone's country like this

https://graph.microsoft.com/v1.0/users/<userid>?$select=Country  

or this

https://graph.microsoft.com/v1.0/users/<userid>/Country  

I don't get the same information for the same users in Powershell. The Country property does not seem to be set.

PS> $User = Get-MgUser -UserId $SameUserid  
PS> $User.Country  
 *-> nothing returned <-*  
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2022-10-20T20:00:59.083+00:00

    It works using the URL because you are specifically asking for it. If you leave off the $select=country or /Country then you don't get it. That is what Get-MgUser is calling. However that cmdlet let's you specify additional properties to retrieve via the -Property argument. So ask for the country as well. Perhaps something like this:

       Get-MgUser -UserId $SameUserId -Property Country  
    
    0 comments No comments

  2. Christine VACHER 21 Reputation points
    2022-10-21T02:34:04.727+00:00

    I am getting the first part of your answer.

    Unfortunately, Country does not seem to be a property that can be asked for the way you suggest.
    The only properties that I am able to retrieve with -Property are Id DisplayName Mail UserPrincipalName and UserType. Not even Surname (which I can get from the $User object of my question).

    Even more puzzling: This guy here is readily getting the Country property:
    2022-02-17_11h45_45.jpg.webp

    Any idea?


  3. Zehui Yao_MSFT 5,876 Reputation points
    2022-10-21T10:10:35.223+00:00

    Hi @Christine VACHER , here are some examples of my success running locally, the same is used in the documentation.
    I think fl is a kind of shortcut to Format-List in what you're sharing. Hope it can help you.

    Get-MgUser -UserId <string>| Format-List  ID, DisplayName, Mail, UserPrincipalName, Country  
    

    252975-image.png

    253003-image.png

    252968-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  4. Christine VACHER 21 Reputation points
    2022-11-27T01:30:03.443+00:00

    Thank you @Zehui Yao_MSFT .
    This worked for me (getting canadian team members):

    Get-InstalledModule Microsoft.Graph  
      
    Select-MgProfile -Name beta  
      
    Connect-MgGraph -Scopes Group.Read.All, User.Read  
    $OurTeam = Get-MgGroup -Filter "DisplayName eq 'Our Team'"  
    $OurTeamMembersId = Get-MgGroupTransitiveMember -GroupId $OurTeam.Id -Property Id  
    # One cannot filter Get-MgUser on Country since it is not a default property  
    $OurTeamMembersId | ForEach-Object { Get-MgUser -UserId $_.Id } | Where-Object Country -eq 'Canada' | Select-Object DisplayName,UserPrincipalName  
    
    0 comments No comments

  5. Andreea Ciocoiu 0 Reputation points
    2023-05-24T07:36:35.1966667+00:00

    So I used @Zehui Yao_MSFT answer, it shows only the property, not what is stored in it. As in: User's image

    In Azure Portal I have as followed: User's image

    and User's image

    Any ideas why it won't work or if there are other ways?

    PS: permissions are set as recommended by Microsoft on API

    0 comments No comments

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.