# Get-ADComputer Script Best Practice

**URL:** <https://forums.powershell.org/t/get-adcomputer-script-best-practice/2298>\
**Category:** PowerShell Help\
**Created:** [February 28, 2014, 7:32am UTC](https://forums.powershell.org/t/get-adcomputer-script-best-practice/2298 "2014-02-28T07:32:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![david-deherrera](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@david-deherrera](https://forums.powershell.org/u/david-deherrera)\
**Post date:** [February 28, 2014, 7:32am UTC](https://forums.powershell.org/t/get-adcomputer-script-best-practice/2298/1 "2014-02-28T07:32:06Z")

</div>

I am having an issue on adding a variable to my Get-ADComputer script.  
This one is failing:  
`$PartialName = “3v1*”
Get-ADComputer -Filter ‘Name -like “$PartialName”’ | select -ExpandProperty Name`

Here are the two ways I have been able to get it to work:  
`$PartialName = “3v1*”
Get-ADComputer -Filter “" | ? {$_.Name -like “$PartialName”} | select -ExpandProperty Name`  
_Or  
 `$PartialName = "3v1`_`”
Get-ADComputer -Filter “Name -like “”$PartialName”“” | select -ExpandProperty Name`

What is the best way of doing this? Less resources, more efficient…etc?

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [February 28, 2014, 7:35am UTC](https://forums.powershell.org/t/get-adcomputer-script-best-practice/2298/2 "2014-02-28T07:35:46Z")

</div>

Well, there’s mainly “the way that works.”

In your first example, you’re sending the literal $PartialName to Active Directory. It has no idea what to do with it. In your last example, by surrounding the filter in double quotes, you’re allowing PowerShell to replace $PartialName with its contents, before sending the filter to AD. So that works.

Your second example is querying every single computer from AD and then filtering them locally. I suspect that will not perform as well as the second one.

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [February 28, 2014, 8:36am UTC](https://forums.powershell.org/t/get-adcomputer-script-best-practice/2298/3 "2014-02-28T08:36:01Z")

</div>

Did you try

$PartialName = “3v1\*”

Get-ADComputer -Filter “Name -like ‘$PartialName’” | select -ExpandProperty Name

Alternatively use an LDAP filter - I think this should work & can’t test it at the moment

Get-ADComputer -LDAPFilter “(cn=$PartialName)”

---

<div class="post-metadata">

**Author:** ![david-deherrera](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@david-deherrera](https://forums.powershell.org/u/david-deherrera)\
**Post date:** [March 3, 2014, 12:44am UTC](https://forums.powershell.org/t/get-adcomputer-script-best-practice/2298/4 "2014-03-03T00:44:11Z")

</div>

@Don Jones - That makes sense, I am just confused as why the variable wasn’t working.

@Richard Siddaway - That worked like a champ. So I watched Jason Helmick and Jeffrey Snover, on the MVA PowerShell and he explained that you can put a variable in between "'s (“Here is $Variable”) and it would stay a variable, and anything between the ’ would be plain text. So why is this working?

Thank you both for helping me out with this.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 3, 2014, 1:03am UTC](https://forums.powershell.org/t/get-adcomputer-script-best-practice/2298/5 "2014-03-03T01:03:13Z")

</div>

“Name -like ‘$PartialName’” is a double-quoted string; any variables inside of it get expanded. The single quotes inside that string are just text; they don’t receive any special treatment from PowerShell, and don’t affect the behavior of variable expansion.

The reverse applies as well: ‘Name -like “$PartialName”’ is a single-quoted string. It doesn’t matter that the string happens to contain double quotes; the variable is still not going to be expanded.

---

<div class="post-metadata">

**Author:** ![david-deherrera](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@david-deherrera](https://forums.powershell.org/u/david-deherrera)\
**Post date:** [March 3, 2014, 8:16am UTC](https://forums.powershell.org/t/get-adcomputer-script-best-practice/2298/6 "2014-03-03T08:16:18Z")

</div>

@Dave Wyatt - Thank you sir.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:52pm UTC](https://forums.powershell.org/t/get-adcomputer-script-best-practice/2298/7 "2024-05-16T20:52:23Z")

</div>


