Unable to use Array Item in invoke-sqlcmd

I am trying to retrieve records from a sql server table using below query. I do not get any error. I will post both the working and non-working parts of code , so that you could tell me why the non-working code failed ( syntax ?)
Non-Working code
$sql=“select * from tablename where colname=‘$ArrayVar[$index].propertyname’”
invoke-sqlcmd -query $sql -serverinstance xxx -database xxxx
Working code
$var=ArrayVar[$index].propertyname
$sql=“select * from tablename where colname=‘$var’”
invoke-sqlcmd -query $sql -serverinstance xxx -database xxxx

Pardha,
Welcome to the forum. :wave:t4:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

I’m not sure if it’s a typo or a copy and paste error and you don’t share the relevant code and I’m not familiar with SQL queries but you could try to use the subexpression operator for the variable inside your query string.

$sql = "select * from tablename where colname='$($ArrayVar[$index].propertyname)'"
invoke-sqlcmd -query $sql -serverinstance xxx -database xxxx