based in my knowledge we cannot use a single quote with a variable (dynamic data) but in some cases the single quote is required ,so my question is there any way to make second code works?
the code without variable is working fine:-
Get-ADUser -Filter 'Name -like "david*"'
the code with variable not returning any data:-
$fname = Read-Host -Prompt 'Input the employee first name'
Get-ADUser -Filter 'Name -like "$fname*"'
You can format the filter query using –f
operator.
PS> $CustomFilter = 'Name -like "{0}*"' -f $fname
PS> $CustomFilter
Name -like "david*"
PS> Get-ADUser -Filter $CustomFilter