Problems with Get-ADUser -Filter

137 Views Asked by At

I want to know how many Users whose ending are two numbers there are where the Attribute "UserParameters" is null. This is my command so far:

$var1 = Get-ADUser -Filter {SamAccountName -like "*[0-9][0-9]"} -SearchBase "OU=xxxxxxxxxxxxxx,DC=xxxxxxx,DC=xx" | Where-Object {$_.UserParameters -eq $null}

If I type in $var1.count it just replies with 0. Why is that so? And yes there are users that end with two numbers. I think the -Filter is the Problem because if I just type -Filter * it works just fine. Please help me!

1

There are 1 best solutions below

2
Santiago Squarzon On

The AD Filter provider has no clue what the wildcard [0-9] means. You can construct an LDAP Filter yourself if needed which would essentianlly be an OR pattern |:

(|(samAccountName=*00)(samAccountName=*01)(samAccountName=*02)(samAccountName=*03)...)

For example:

$filter = "(|"
foreach($i in 0..9) {
    foreach($x in 0..9) {
        $filter += "(samAccountName=*$i$x)"
    }
}
$filter += ")"
Get-ADUser -LDAPFilter $filter -SearchBase 'OU=xxxxxxxxxxxxxx,DC=xxxxxxx,DC=xx'