'Powershell Get-Package not filtering properly

I am having this issue with filtering of application with Get-Package cmdlet

when I use the below code,

$Cred = Get-Credential

Invoke-Command -ComputerName "server01" -Credential $Cred -ScriptBlock { Get-Package -Name  "Google Chrome"}

it is properly filtering out Chrome from the remote machine.(see below)

enter image description here

if I assign Chrome to a variable, like below

$Cred = Get-Credential
$app = "Google Chrome"
Invoke-Command -ComputerName "server01" -Credential $Cred -ScriptBlock { Get-Package -Name  $app}

Its showing all the applications installed in the remote machine.

enter image description here

What seems to be the issue here ? Is it not the proper way to assign variable?



Solution 1:[1]

I got it. Had to use $Using in front of variable

$Cred = Get-Credential
$app = "Google Chrome"
Invoke-Command -ComputerName "server01" -Credential $Cred -ScriptBlock { Get-Package -Name  $Using:app}

This worked.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sid133