Forum Discussion

AwsAyad's avatar
AwsAyad
Brass Contributor
May 05, 2022
Solved

How to get all Azure AD users with numeric UserPrincipalName using PowerShell ?

Hello everyone, I'm trying to get a list of all active accounts in Azure AD where the UPN is all numeric and has no letters at all (i.e. mailto:123456@mydomain.com)  ? I used this line of code to no ...
  • LainRobertson's avatar
    May 06, 2022

    AwsAyad 

     

    Hi,

     

    Your regular expression is incorrect. What you're currently looking for is a string consisting of numbers only, which in the Azure AD userPrincipalName context since it contains "@" and probably characters after the "@" that aren't numeric.

     

    You want to change it to:

     

    Get-AzureADUser -All $true | Where-Object {$_.UserPrincipalName -match '^\d+@'} | select DisplayName, UserPrincipalName | Export-Csv C:\Temp\Numeric_Accounts.csv -NoTypeInformation

     

    Cheers,

    Lain

Resources