Home > Blockchain >  How to find UPN that contains digit?
How to find UPN that contains digit?

Time:08-07

i would like to know how to find UPN that constain digit with -filter?

Get-ADUser -filter {(UserPrincipalName -contains "I dont know what i should add here @contoso.com")} -properties userprincipalname | select userprincipalname

CodePudding user response:

The -Filter argument of AD cmdlets, which accepts a string, uses PowerShell-like syntax, but with only a limited subset of supported operators, some of which work in subtly different ways than in PowerShell.

The filter language is not sophisticated enough to do the matching you want: the only pattern matching supported is via wildcards, which are limited to use of *, using the -like operator.[1]

Therefore, use -Filter for pre-filtering with -like, then use a Where-Object call to let PowerShell filter the results down, using its regex capabilities:

Get-ADUser -Filter 'UserPrincipalName -like "*@contoso.com"' -Properties UserPrincipalName | 
  Where-Object UserPrincipalName -match '\d'
  Select-Object UserPrincipalName

Note:

  • -match '\d' matches if at least one digit (\d) is present in the input.

  • I've used a string rather than a script block ({ ... }) to specify the -Filter argument, because that's what -Filter expects. While seductively convenient, the use of script blocks is conceptually problematic and can lead to misconceptions - see this answer.


[1] By contrast, PowerShell's -like operator supports PowerShell's more fully-featured wildcard expressions. Also, the AD -Filter's language at least situationally interprets * to mean: at least one character, whereas PowerShell's wildcard expression interpret it as zero or more.

  • Related