Forum Discussion
AwsAyad
May 05, 2022Brass Contributor
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 ...
- May 06, 2022
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
May 06, 2022Silver 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
- AwsAyadMay 06, 2022Brass Contributor
LainRobertson Thank you, this did fix the issue with my expression. I appreciate it.