May 05 2022 09:44 AM - edited May 05 2022 09:45 AM
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
May 05 2022 07:14 PM
Solution
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
May 06 2022 04:42 PM
@LainRobertson Thank you, this did fix the issue with my expression. I appreciate it.