Using a local variable in a remote PowerShell session

While working with remote computers with PowerShell, you'll come across many scenarios where you may want to use local variables or data in a remote session. But when you try to use a local variable in a remote session, it may not work or may throw an error because the local variable doesn't exist in the remote session.

To overcome this gap, PowerShell provides three methods to pass local variables to remote sessions. You can use them with Invoke-Command and New-PSSession:

  1. The $args automatic variable
  2. The $using scope modifier
  3. The param() block
Local variable in a remote session

Local variable in a remote session

Make sure to get the user credentials and choose a computer name before using the commands in the following methods.

Using the $args automatic variable ^

The $args variable is an automatic variable that contains an array of the undeclared parameters or parameter values passed to a function or a scriptblock. You can access the parameters passed to the scriptblock just like elements of an array.

The first parameter passed to the scriptblock is $args[0], the second is $args[1], and so on. That means we can use $args in the scriptblock and run it remotely via the Invoke-Command cmdlets. We can pass the parameters through the -ArgumentList parameter like in the following example:

Using the $args automatic variable

Using the $args automatic variable

Parameterized scriptblocks using param() ^

You can use param() blocks inside a function to accept parameters, but the only difference between a function and a scriptblock is that a function is a named scriptblock. That means that just like in a function, we can use a param() block in a scriptblock. Then we will use this scriptblock with the Invoke-Command cmdlet and pass parameters through the -ArgumentList.

Using a parameterized scriptblock

Using a parameterized scriptblock

The $using scope modifier ^

You can use local variables in remote sessions, but you must indicate that the variable is defined in the local session. Since Windows PowerShell 3.0, you can use the $using scope modifier to indicate that the variable defined in remote session is also defined in the local session, like in the following example:

The $using scope modifier

The $using scope modifier

You can also change the scope of the local variable in PSSessions with the $using scope modifier:

Changing scope in PSSession

Changing scope in PSSession

Conclusion ^

In PowerShell 2, you could use local variables in remote PowerShell sessions by passing arguments to the remote scriptblock and capturing them inside the scriptblock with either the $args automatic variable or param() blocks. But since Windows PowerShell v3, we can use the scope modifier $using to change the scope of local variables. This makes them work in the remote session very easily without passing any arguments and capturing the arguments or parameters.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

2+
avataravatar
1 Comment
  1. MikeC 2 years ago

    This is great.  Thanks. I have always passed them using -ArgumentList  great tip!  Thanks.

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account