SOLVED

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

Brass Contributor

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. 123456@mydomain.com)  ? I used this line of code to no avail, I am getting no results.

 

Thoughts ?

 

 

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

 

 

 

 

 

2 Replies
best response confirmed by AwsAyad (Brass Contributor)
Solution

@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

@LainRobertson Thank you, this did fix the issue with my expression. I appreciate it.

1 best response

Accepted Solutions
best response confirmed by AwsAyad (Brass Contributor)
Solution

@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

View solution in original post