skip to Main Content

I cannot retrieve all the data that I want even though I can see that the data exists while I’m viewing through the portal.

$users = Get-AzureADUser -All $true | `
    Where-Object {$_.CompanyName -like 'CompanyName*' | `
    Get-Random -Count 1000 | `
    Select-Object @{N="EmployeeID";E={$_.ExtensionProperty.employeeId}}, GivenName, Surname, OfficeLocation

I’ve changed the Company name and removed some of the objects I’m retrieving for privacy reasons.
This script above is used in my Powershell window and it successfully retrieves the data I want.

EmployeeId did not work at first and by googling and testing around I found that I have to use the "ExtensionProperty" in order to retrieve that data. The problem I’m having now is that I cannot retrieve "OfficeLocation" no matter what I try.

I’ve double-checked to make sure that I have proper access to my account and all seems to be in order, why can I not retrieve any data for "officeLocation"?


I have tried the following

  • I tried using a variety of names for OfficeLocation.. examples are = OfficeLocation, officeLocation, Office, office_location, Office_Loc, Location etc etc… I’ve spent almost two days trying to figure this out.

I tried writing:

@{N="OfficeLocation";E={$_.AdditionalProperties["officeLocation"]}}

I’ve also tried:

@{N="OfficeLocaton";E={$_.ExtensionProperty.officeLocation}}

Neither works.
This is how the field looks in my AzureAD

So in this example (I’ve covered the actual location) I can see that data exists about the "officelocation" but I cannot retrieve it by using my PowerShell script.

Is this not possible through Powershell? Do I need to use Microsoft Graph API?

Thankful for any assistance or guidance you can point me towards!

Edit Spelling

3

Answers


  1. I think that you need to update the PowerShell module for Azure because the last version has a different command: Get-AzADUser.

    Try this:

    Update-Module -Name Az -Force
    

    And then:

    $users = Get-AzADUSer  | `
        Where-Object {$_.CompanyName -like 'CompanyName*' | `
        Get-Random -Count 1000 | `
        Select-Object @{N="EmployeeID";E={$_.ExtensionProperty.employeeId}}, GivenName, Surname, OfficeLocation
    
    Login or Signup to reply.
  2. The correct syntax for the piped object is $_, not just $. So if nothing else, try:

    @{n="EmployeeID";n={$_.ExtensionProperty.employeeId}}
    

    To see the actual keys within the extension property object I suggest:

    (Get-AzureADUser | select -first 1).ExtensionProperty.Keys
    
    Login or Signup to reply.
  3. Note that, OfficeLocation is displayed in PhysicalDeliveryOfficeName property while using Azure AD PowerShell v2 module.

    In your case, you need to modify your code by replacing OfficeLocation with PhysicalDeliveryOfficeName like below:

    $users = Get-AzureADUser -All $true | `
        Where-Object {$_.CompanyName -like 'CompanyName*' | `
        Get-Random -Count 1000 | `
        Select-Object @{N="EmployeeID";E={$_.ExtensionProperty.employeeId}}, GivenName, Surname, PhysicalDeliveryOfficeName
    

    Response:

    enter image description here

    Alternatively, you can make use of Microsoft Graph API to fetch users by running below query:

    GET https://graph.microsoft.com/v1.0/users?$filter=companyName in ('CompanyName')&$select=givenName,surname,officeLocation,employeeId&$count=true
    ConsistencyLevel: Eventual
    

    Response:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search