1
Import-Module activedirectory

[string]$Name = "Larry Page"
Get-ADUser -Filter 'Name -like "$Name"'

How do I get the name into the variable? At execution, it doesn't appear to substitute the name at runtime

software is fun
  • 7,286
  • 18
  • 71
  • 129
  • https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6#single-and-double-quoted-strings – Bacon Bits May 21 '19 at 20:12
  • 1
    It's not an issue with assigning the name to the variable, it's with how your're writing your filter. The variable is being interpreted literally. You need to escape the variable: https://stackoverflow.com/questions/20075502/get-aduser-filter-will-not-accept-a-variable – TheRealPalpatine May 21 '19 at 20:14
  • Possible duplicate of [Get-Aduser -Filter will not accept a variable](https://stackoverflow.com/questions/20075502/get-aduser-filter-will-not-accept-a-variable) –  May 21 '19 at 20:15

1 Answers1

8

You have the quotes flipped. Variable substitution only happens with double quoted strings. The first set of single quotes tells PowerShell to not do the substitution. If you use double quotes on the outside, you can use the singles inside and still get the substitution.

Import-Module activedirectory

[string]$Name = "Larry Page"
Get-ADUser -Filter "Name -like '$Name'"
HAL9256
  • 12,384
  • 1
  • 34
  • 46