Category Archives: PS Tip

PS Tip – Use local variable in Invoke-Command (remote computer)

I have a locally declared variable $var1

Using it directly doesn’t work, the value of the variable is not carried over within the invoke-command scriptblock.

$result2 = Invoke-Command -ComputerName $computer {(gwmi Win32_NetworkAdapter | where {$_.InterfaceIndex -eq $var1}).MACAddress}

Instead you have to use the “Using” scope modifier followed by a colon, thus $var1 becomesĀ $using:var1

$result2 = Invoke-Command -ComputerName $computer {(gwmi Win32_NetworkAdapter | where {$_.InterfaceIndex -eq $using:var1}).MACAddress}