Forum Discussion
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 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
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
2 Replies
- LainRobertsonSilver Contributor
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
- AwsAyadBrass Contributor
LainRobertson Thank you, this did fix the issue with my expression. I appreciate it.