Powershell Start-Process to start Powershell session and pass local variables Powershell Start-Process to start Powershell session and pass local variables powershell powershell

Powershell Start-Process to start Powershell session and pass local variables


I'm pretty sure there's no direct way to pass variables from one PowerShell session to another. The best you can do is some workaround, like declaring the variables in the code you pass in -ArgumentList, interpolating the values in the calling session. How you interpolate the variables into the declarations in -ArgumentList depends on what types of variables. For an array and a string you could do something like this:

$command = '<contents of your scriptblock without the curly braces>'Start-Process powershell -ArgumentList ("`$Array = echo $Array; `$String = '$String';" + $command)


You could wrap the contents of your script block in a function, and then call the function from the ArgumentList and pass in the variables as parameters to the function, as I do on this post.

$ScriptBlock = {    function Test([string]$someParameter)    {        # Use $someParameter to do something...    }}# Run the script block and pass in parameters.$myString = "Hello"Start-Process -FilePath PowerShell -ArgumentList "-Command & {$ScriptBlock Test('$myString')}"


The command line options for PowerShell.exe say that you should be able to pass arguments when using a script block by adding -args:

PowerShell.exe -Command { - | <script-block> [-args <arg-array>] | <string> [<CommandParameters>] }

However when I try to do that I get the following error:

-args : The term '-args' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I added $MyInvocation | fl to the script block to see what was happening, and it looks like the -args is just appended to the deserialized commands in the script block (hence the error since -args is not a valid command). I also tried using GetNewClosure() and $Using:VariableName but those only appear to work when the script block is invoked (as opposed to this where we are using it to serialize/deserialize the commands).

The I was able to get it to work by wrapping it in a function like deadlydog's answer.

$var = "this is a test"$scriptblock = {    $MyInvocation | fl #Show deserialized commands    function AdminTasks($message){        write-host "hello world: $message"    }}Start-Process powershell -ArgumentList '-noexit','-nologo','-noprofile','-NonInteractive','-Command',$scriptblock,"AdminTasks('$var')" -Verb runAs #-WindowStyle Hidden#Output:MyCommand             :                         $MyInvocation | fl #Show deserialized commands                         function AdminTasks($message){                         write-host hello world: $message                         }                         AdminTasks('this is a test')BoundParameters       : {}UnboundArguments      : {}ScriptLineNumber      : 0OffsetInLine          : 0HistoryId             : 1ScriptName            :Line                  :PositionMessage       :PSScriptRoot          :PSCommandPath         :InvocationName        :PipelineLength        : 2PipelinePosition      : 1ExpectingInput        : FalseCommandOrigin         : RunspaceDisplayScriptPosition :hello world: this is a test

Wrapping it in a script block and using $args[0] or $args[1] also works, just be aware that you many need to wrap the $var0 or $var1 in quotes if there are issues when it is deserialized and use `$ to prevent the $sb from being replaced with "" since that variable doesn't exist in the caller's scope:

$var0 = "hello"$var1 = "world"$scriptblock = {    $MyInvocation | fl #Show deserialized commands    $sb = {        write-host $args[0] $args[1]    }}Start-Process powershell -ArgumentList '-noexit','-nologo','-noprofile','-NonInteractive','-Command',$scriptblock,"& `$sb $var0 $var1"