Export all users to a CSV file with all OnPremisesExtensionAttributes

Bühler Gabriel 81 Reputation points
2023-01-24T14:15:01.5266667+00:00

Hey Guys

I am trying to set up a small script that exports all my AzureAD Users with all their ExtensionAttributes into a CSV-File. Best would be to have a Line for each User with all their Properties. Here is what I have so far:

 Get-MgUser  | Where-Object {$_.UserPrincipalName -Like "*companyname.com"} | Select-Object DisplayName,onPremisesExtensionAttributes -ExpandProperty onPremisesExtensionAttributes | Format-Table DisplayName, ExtensionAttribute1, ExtensionAttribute2, ExtensionAttribute3, ExtensionAttribute4, ExtensionAttribute5, ExtensionAttribute6, ExtensionAttribute7, ExtensionAttribute8, ExtensionAttribute9, ExtensionATtribute10, ExtensionAttribute11, ExtensionAttribute12, ExtensionAttribute13, ExtensionAttribute14, ExtensionAttribute15 | Export-CSV -Append -Delimiter ";"

But it unfortunately just shows up empty...do you may see what I am doing wrong?

Thank you for your help.

Kind regards,

Gabe

Windows for business Windows Server User experience PowerShell
Microsoft Security Microsoft Entra Microsoft Entra ID
Microsoft Security Microsoft Graph
{count} votes

Accepted answer
  1. Vasil Michev 119.5K Reputation points MVP Volunteer Moderator
    2023-01-24T14:43:16.88+00:00

    For starters, you need to specifically request the properties, as by default Get-MgUser returns only a small subset.

    Get-MgUser -Property DisplayName,onPremisesExtensionAttributes,UserPrincipalName

    Do note that you have to request each property you plan to use, including those used for filtering. Next, don't use Format-table, as it does not mix with Export-CSV. Use another select statement instead, which gives us:

    Get-MgUser -Property DisplayName,onPremisesExtensionAttributes,UserPrincipalName | Where-Object {$_.UserPrincipalName -Like "*michev.onmicrosoft.com"}| Select-Object -ExpandProperty onPremisesExtensionAttributes | select DisplayName,UserPrincipalName,ExtensionAttribute* | Export-CSV -nti blabla.csv

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.