# Problem using variable as parameter in cmd-let in a script

**URL:** <https://forums.powershell.org/t/problem-using-variable-as-parameter-in-cmd-let-in-a-script/5854>\
**Category:** PowerShell Help\
**Created:** [February 25, 2016, 1:39am UTC](https://forums.powershell.org/t/problem-using-variable-as-parameter-in-cmd-let-in-a-script/5854 "2016-02-25T01:39:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacobsenmic](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jacobsenmic](https://forums.powershell.org/u/jacobsenmic)\
**Post date:** [February 25, 2016, 1:39am UTC](https://forums.powershell.org/t/problem-using-variable-as-parameter-in-cmd-let-in-a-script/5854/1 "2016-02-25T01:39:22Z")

</div>

Hi,  
Can someone help me please?

I can’t get this to Work:

#1

$strSearchBase = $strOU + $srtDomain

$Computers = get-adcomputer -filter \* -searchbase $strSearchBase | select dnshostname

I get this error:  
get-adcomputer : Directory object not found

But this Works perfectly:

#2

$strSearchBase = “ou=Database servere, ou=Domain Servere, dc=egedal, dc=egekom, dc=org”

$Computers = get-adcomputer -filter \* -searchbase $strSearchBase | select dnshostname

In #1 when I make Write-host $strSearchBase I get this in the console:

ou=Domain Servere, ou=Database servere, dc=egedal, dc=egekom, dc=org

So the two string should be tha same???

---

<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:** [February 25, 2016, 1:57am UTC](https://forums.powershell.org/t/problem-using-variable-as-parameter-in-cmd-let-in-a-script/5854/2 "2016-02-25T01:57:49Z")

</div>

Depends on what’s in your $strOU and $srtDomain variables. (And is that a typo, supposed to be $strDomain?)

---

<div class="post-metadata">

**Author:** ![jacobsenmic](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jacobsenmic](https://forums.powershell.org/u/jacobsenmic)\
**Post date:** [February 25, 2016, 2:52am UTC](https://forums.powershell.org/t/problem-using-variable-as-parameter-in-cmd-let-in-a-script/5854/3 "2016-02-25T02:52:39Z")

</div>

@Dave Wyatt

$strSearchBase = $strOU + $srtDomain  
Write-host $strSearchBase

ou=Domain Servere, ou=Database servere, dc=egedal, dc=egekom, dc=org

---

<div class="post-metadata">

**Author:** ![jacobsenmic](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jacobsenmic](https://forums.powershell.org/u/jacobsenmic)\
**Post date:** [February 25, 2016, 2:59am UTC](https://forums.powershell.org/t/problem-using-variable-as-parameter-in-cmd-let-in-a-script/5854/4 "2016-02-25T02:59:58Z")

</div>

Yes it’s a typo, but this is not the cause of the problem 🙂

---

<div class="post-metadata">

**Author:** ![jacobsenmic](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jacobsenmic](https://forums.powershell.org/u/jacobsenmic)\
**Post date:** [February 25, 2016, 3:26am UTC](https://forums.powershell.org/t/problem-using-variable-as-parameter-in-cmd-let-in-a-script/5854/5 "2016-02-25T03:26:46Z")

</div>

I still can’t get it to work

---

<div class="post-metadata">

**Author:** ![aapeli-hietikko](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aapeli-hietikko](https://forums.powershell.org/u/aapeli-hietikko)\
**Post date:** [February 25, 2016, 9:18am UTC](https://forums.powershell.org/t/problem-using-variable-as-parameter-in-cmd-let-in-a-script/5854/6 "2016-02-25T09:18:18Z")

</div>

Looks to be absolutely correct, if your variables are as follows. Remember the last , in $strOU.

```
$strOU = 'ou=Domain Servere, ou=Database servere, '
$srtDomain = 'dc=egedal, dc=egekom, dc=org'
$strSearchBase = $strOU + $srtDomain
```

You could also combine your variables with double quotes. That shouldn’t make a difference but it looks nice and you can also drop that trailing space from $strOU.  
As if the spaces would even matter in this case.

```
$strOU = 'ou=Domain Servere, ou=Database servere,'
$srtDomain = 'dc=egedal, dc=egekom, dc=org'
$strSearchBase = "$strOU $srtDomain"
```

---

<div class="post-metadata">

**Author:** ![jacobsenmic](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jacobsenmic](https://forums.powershell.org/u/jacobsenmic)\
**Post date:** [February 26, 2016, 1:08am UTC](https://forums.powershell.org/t/problem-using-variable-as-parameter-in-cmd-let-in-a-script/5854/7 "2016-02-26T01:08:42Z")

</div>

It’s working now. I had accidently reversed the order of the OU’s in the strOU string.

Thanks for the input guys 🙂

---

<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:42pm UTC](https://forums.powershell.org/t/problem-using-variable-as-parameter-in-cmd-let-in-a-script/5854/8 "2024-05-16T20:42:45Z")

</div>


