Reference local variables in a remote PowerShell session the easy way

Using variables in remote sessions with PowerShell was tough before PowerShell 3. Learn the easy way.

John Savill

January 24, 2016

1 Min Read
Reference local variables in a remote PowerShell session the easy way

Q. How can I easily reference local variables in a remote PowerShell execution?

A. PowerShell v3 and above makes this very easy. Prior to version 3 to use variables in a remote PowerShell session you needed to pass the variables as arguments for example:

$Name = "John"
Invoke-Command -ComputerName savxvinode1 -ScriptBlock {Write-Output "Hello $args"} -ArgumentList $Name

In PowerShell v3 you can leverage $using: to access local variables, for example:

$Name = "John"
Invoke-Command -ComputerName savxvinode1 -ScriptBlock {Write-Output "Hello $using:name"}

Each week, John Savill answers all of your toughest tech questions about the worlds of Windows Server, Azure, and beyond. Read his past IT advice here, and email your questions to [email protected].

About the Author(s)

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like