PS7 shows Variable: found in expression: is not defined PS5.1 works fine

Hi
I`m trying to run a query od AD:

<!–StartFragment -->
$GroupNameLike = Read-Host “Enter group name:”</div>
(Get-AdGroup -filter {name -like $GroupNameLike} | select name -expandproperty name)
<!–EndFragment --></div>

This code works fine on PS5.1 but on PS7 i got an error:

<!–StartFragment -->
Get-ADGroup: Variable: ‘GroupNameLike’ found in expression: $GroupNameLike is not defined.
<!–EndFragment -->

I found that PS6 and above need to have declered Variables, but all the ways to do it doesn`t work. I was trying to do it like that:

<!--StartFragment -->
(Get-AdGroup -filter -ScritBlock {name -like $Using:GroupNameLike} | select name -expandproperty name)
Get-AdGroup -filter [ScritBlock]{param($GroupNameLike) name -like $GroupNameLike} | select name -expandproperty name
Get-AdGroup -filter -ScritBlock {param($GroupNameLike) name -like $GroupNameLike} | select name -expandproperty name
Get-AdGroup -filter {param($GroupNameLike) name -like $GroupNameLike} | select name -expandproperty name
Get-AdGroup -filter -ScriptBlock {name -like $Using:GroupNameLike} | select name -expandproperty name
Get-AdGroup -filter -ScriptBlock {param($GroupNameLike) name -like $GroupNameLike} | select name -expandproperty name
<!--EndFragment -->
I`m not an expert in PowerShell so sorry if this is a trivial question.
 

Your code should work fine, my tests did. But try this instead and see if it works for you.

$GroupNameLike = Read-Host "Enter group name"
Get-AdGroup -filter "name -like '$GroupNameLike'" | select -expandproperty name

If you’re wanting to allow incomplete group names to be entered, you may want to add asterisks like this.

$GroupNameLike = Read-Host "Enter group name"
Get-AdGroup -filter "name -like '*$GroupNameLike*'" | select -expandproperty name

If you’re wanting the value of name, leave it as just Select -ExpandProperty name. If you want an object with a name property, change it to Select Name

Thank you very much, changing {} to “” and puting varibable in ‘’ did the trick. Your the best!!