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

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:

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:

Like this:

About 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.