Get-ADGroup with wildcards

  • Using the Quest AD cmdlets, this easily returns all groups with underscores around the $Code.

    Get-QADGroup "*`_$Code`_*"

    How can this be done with Get-ADGroup (ie, not using the Quest cmdlets, which not everyone has).

    The -Filter parm only accepts the * wildcard. Help says to use LDAPFilter parm, but I can't seem to get it right:

    Get-ADGroup -LDAPFilter {(name="*_$Code_*")} is not working

    Thanks!

  • this is working for me:

    Import-Module ActiveDirectory

    Get-ADGroup -Filter {Name -like '*SQL*'} -Properties * | select -property SamAccountName,Name,Description,DistinguishedName,CanonicalName,GroupCategory,GroupScope,whenCreated

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • $Code = "GOL"

    This returns all the groups having "GOL" in their names:

    Get-QADGroup "*$Code*"

    This returns all the groups having _GOL_ in their names:

    Get-QADGroup "*`_$Code`_*"

    Both of these return nothing:

    Get-ADGroup -Filter {Name -like '*$Code*'}

    Get-ADGroup -Filter {(name -like "*$Code*")}

  • My guess is that the Get-QADGroup cmdlet detects the variable in the middle of the search string, whereas Get-ADGroup treats it as a literal. You'll need to find a way to build the string so that it preserves the variable.

    John

  • John: You're right about the variable not substituting. The literal "*_GOL_*" returns the correct list.

    Any ideas how to make this work?

  • I imagine you'll have to concatenate, something like this:

    '*' + $Code + '*'

    I don't use Powershell all that much so I'm afraid I don't know the exact syntax off the top of my head.

    John

  • Got it:

    $Code = "*_GOL_*"

    Get-ADGroup -Filter {(name -like $Code)} | select -ExpandProperty name

    Thanks!

  • guys....

    String: "blah blah blah"

    text: 'blah blah blah'

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply