#PSTip Passing local variables to a remote session in PowerShell 3.0

Note: This tip requires PowerShell 3.0 or above.

Passing local variables to a remote session has become a lot easier in PowerShell 3.0 compared to previous versions. Let’s have a look at a few examples. Consider the following variables defined in your local PowerShell session:

$a = 'PowerShell'
$b = 'Rocks'

If you want to use these variables in a remote session, you will see that the following will not work:

$a = 'PowerShell'
$b = 'Rocks'

Invoke-Command -ComputerName Server01 -ScriptBlock {
   Write-Output The value of variable a is: $a
   Write-Output The value of variable b is: $b
}

The value of $a is:
The value of $b is:

In PowerShell 2.0 we had two options in order to pass our local variables to the remote session. The first option was to pass on the variables to the –ArgumentList parameter. We then had to pick them up from the $args array, and keep track of the order we passed the variables in. For example, the first variable specified on the –ArgumentList parameter could be picked up accessing $args[0], the second variable accessing $args[1], and so on. As we can see in the following example:

Invoke-Command -ComputerName Server01 -ScriptBlock {
   Write-Output The value of variable a is: $($args[0])
   Write-Output The value of variable b is: $($args[1])
} -ArgumentList $a,$b

The value of $a is: PowerShell
The value of $b is: Rocks

The second option was to use the –ArgumentList parameter and a param block:

Invoke-Command -ComputerName Server01 -ScriptBlock {
   param ($first,$second)

   Write-Output The value of variable a is: $first
   Write-Output The value of variable b is: $second
} -ArgumentList $a,$b

The value of $a is: PowerShell
The value of $b is: Rocks

In PowerShell 3.0 we can simply use the Using scope modifier followed by a colon and the name of the local variable we want to reference:

Invoke-Command -ComputerName Server01 -ScriptBlock {
   Write-Output The value of variable a is: $using:a
   Write-Output The value of variable b is: $using:b
}

The value of $a is: PowerShell
The value of $b is: Rocks

About the author: Jan Egil Ring

Jan Egil works as a Lead Architect on the R&D Team at Crayon, Norway. He mainly works with automation, and has a strong passion for PowerShell. Products and services he works with includes Microsoft infrastructure products such as Windows Server, System Center and Microsoft Azure. He is a multiple-year recipient of Microsoft Most Valuable Professional Award for his contributions in the Windows PowerShell and Cloud & Datacenter Management technical communities. He speaks regularly at user groups and conferences, such as Nordic Infrastructure Conference (NIC), PowerShell Conference Europe and PowerShell Summit. He is a co-founder of the Norwegian Microsoft Technology User Group (MTUG), which is an association of local MTUG user groups in Norway. He is also co-organizing the MTUG Script Club which focuses mainly on PowerShell. You can follow him on Twitter @JanEgilRing.

Related Posts

%d bloggers like this: