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.

1
  • 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. Commented Nov 5, 2013 at 5:27

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.