0

Sorry for the simple question to start, but I am stumped on the answer.

My code is simple... I want to take a variable from command line into my script and use that variable as a Filter string within an AD command. I have as follows:

PARAM($myOU)

$FoundOUs = Get-ADOrganizationalUnit -Filter 'Name -like "*"' -SearchBase ="OU=Offices,DC=dc1,DC=domain,DC=com"

So, I want to replace "*" with $myOU... I am at a lost on how to do this. I have tried things like -Filter Name $myOU, etc, but no luck. Any suggestions would be great.

  • I am not sure this is the best option, but this seemed to work (tried it just after posting - should have tried before posting) $FoundOUs = Get-ADOrganizationalUnit -Filter "Name -like '*$($myOU)*'" -SearchBase ="OU=Offices,DC=dc1,DC=domain,DC=com" Anyway, this worked... again, not sure if the best, but it does work. – astromechjapan Nov 05 '13 at 05:27

1 Answers1

1

Use string interpolation like so:

$FoundOUs = Get-ADOrganizationalUnit -Filter "Name -like '$myOU'" -SearchBase="OU=Offices,DC=dc1,DC=domain,DC=com"

Note that string interpolation only happens with double quoted strings so swap the order of single & double quotes so the variable will be interpolated. Also using $($myOU) is unnecessary in this case. You typically use a sub-expression when you need to access a property e.g. $($myOU.Length) or in general evaluate an expression inside a string.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369