0

In the following script:

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | where {$_.DisplayName -like '*Visual*' } | Select-Object -ExpandProperty DisplayName

$productName="Visual"
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | where {$_.DisplayName -like '*$productName*' } | Select-Object -ExpandProperty DisplayName

the first Get-ItemProperty returns the correct results, the second returns nothing.

I tried using a ScriptBlock:

[ScriptBlock]$whereClause = [ScriptBlock]::Create("$_.DisplayName -like '*$productName*'")

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | where $whereClause | Select-Object -ExpandProperty DisplayName

but this errors with "The term '.DisplayName' is not recognized as the name of a cmdlet...."

I've tried various other variations, but I can't seem to get it working and I'm not sure what I'm missing. I would like to be able to use a parameter variable in the Where-Object cmdlet. How do I do this?

1 Answer 1

0

You're trying to use string interpolation with a string constant:

'*$productName*' is a string constant. To use string interpolation in powershell you need to use double quotes:

"*$productName*"
Sign up to request clarification or add additional context in comments.

2 Comments

IMO the more common wording is variable expansion
Thanks. Powershell hurts my head.

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.