I keep seeing many posts with people struggling to execute code on remote machines, it is usually not due to permissions issues, (enable PS Remoting), but mainly due to the fact that they forget that the remote machine does not know the value of the locally assigned variable.

Using (new way) PSv3+

With PowerShell Version 3 and newer, $using was introduced.

If you want to pass a local variable to the remote machine , just add $using: in front of the variable name (e.g. $using:localvariable), and the local value will be given on to the remote machine. This is so much simpler and easier to read than the ‘old’ argumentlist way.

# Using '$using'
# PowerShell v3+
$localvalue01   = 'SampleValue01'
$localvalue02   = 'SampleValue01'

Invoke-Command -ComputerName $remotecomputer -ScriptBlock {
    write-output $using:localvalue01
    write-output $using:localvalue02
}

Argumentlist (old way)

In this sample, we are looking at using an argument list to pass the values of the variable to the remote machine.

You define the values you need per usual on your local machine, and then you add an $argumentlist as parameter.

The order you list the variables is important, as the one first variable listed is addressed with $args[0], the variable next to it (to the right) is addressed with $args[1] and so forth.

# Argument lists
# PowerShell v2 and lower
$localvalue01   = 'SampleValue01'
$localvalue02   = 'SampleValue02'

Invoke-Command -ComputerName $remotecomputer -ScriptBlock {
    write-output $args[0] #holds the value for $localvalue01
    write-output $args[1] #holds the value for $localvalue02
} -ArgumentList $localvalue01, $localvalue02

 

Reference: about_remote_variables