How do I place a variable in a string? with get-ADComputer -filter ... Check out my code and tell me what is wrong.

I'm searching AD with a list of partial computernames. The following works like a charm:
Get-ADComputer -Filter 'Name -like "*comp1*"' | Format-Table Name,DistinguishedName

Output looks like:
Name                           DistinguishedName           
----                              -----------------                                                            
BLA-Comp1-ABC           CN=BLA-Comp1-ABC,OU=WSUS Global Policy,OU=BLABLA,OU=BLABLA

I have a text file of partial computernames I want to call and place in the string portion of the previous code to display the actual computername of all the partials in my list. The following is what I have but its not working. It has something to do with the string and having a variable inside and I can't figure out how to fix it....

Noworking Code
$computers = get-content C:\Scripts\PartialCompNames.csv
 ForEach ($entry in $computers){
 Get-ADComputer -Filter 'Name -like "*$entry*"' | Format-Table Name,DistinguishedName
 }

It seems to be searching for the literal "*$entry*" instead of the data from the CSV as expected.

May 9th, 2013 9:58pm

This worked for me, although I get one table for each computer:

$computers = get-content C:\scripts\PartialCompNames.csv
ForEach ($entry in $computers)
{
    $comp = "*$entry*"
    Get-ADComputer -Filter {Name -like $comp} | Format-Table Name,DistinguishedName
}

-----


Free Windows Admin Tool Kit Click here and download it now
May 9th, 2013 10:18pm

This works for me, and produces one table (rather than one for each computer):

$computers = get-content C:\scripts\PartialCompNames.csv
ForEach ($entry in $computers)
{
    $comp = "*$entry*"
    Get-ADComputer -Filter {Name -like $comp} | Select Name, DistinguishedName
} Format-Table Name, distinguishedName

-----


May 9th, 2013 10:23pm

You just made my day! I really appreciate your help. I learned allot from this.
Free Windows Admin Tool Kit Click here and download it now
May 10th, 2013 1:28pm

This worked for me, although I get one table for each computer:

$computers = get-content C:\scripts\PartialCompNames.csv
ForEach ($entry in $computers)
{
    $comp = "*$entry*"
    Get-ADComputer -Filter {Name -like $comp} | Format-Table Name,DistinguishedName
}

-----

August 31st, 2015 8:34am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics