1

Is it possible to have all the env variables to be passed to a new powershell window started as follows:

$var=10; Start-Process powershell '-command "echo $var; Start-Sleep -s 5; "'

This does not showing anything on the child window.

If it is not possible to have all the variables, how to pass a few of them ?

Is this variable (var) different from env variable ?

Vishwanath
  • 149
  • 2
  • 14
  • Does this answer your question? [Powershell Start-Process to start Powershell session and pass local variables](https://stackoverflow.com/questions/17776426/powershell-start-process-to-start-powershell-session-and-pass-local-variables) – Scepticalist Apr 14 '21 at 11:29
  • is defining a variable and env variable different ? will it help in this case if we define variables as environment variables ? – Vishwanath Apr 14 '21 at 11:38
  • Also, please tell how we can pass variables in current syntax of using -command instead of ArgumentList – Vishwanath Apr 14 '21 at 11:57
  • ``$var=10; Start-Process powershell "-command `"echo $script:var; Start-Sleep -s 5`""`` – Theo Apr 14 '21 at 12:03
  • @Theo i am creating sub-process like this: ``` Start-Process powershell '-command "$Host.UI.RawUI.WindowTitle = ''title''; echo var; Start-Sleep -s 5; ' " ``` – Vishwanath Apr 14 '21 at 13:01

1 Answers1

1

A variable isn't the same as an environment variable. In PowerShell, environment variables are accessed using $ENV For example $ENV:COMPUTERNAME or $ENV:logonserver.

ex. Start-Process Powershell "ECHO $ENV:COMPUTERNAME;Pause"

Since these values are being pulled from the OS when called, they are always available and do not depend on your PowerShell environment.

The variable $var is a session scoped variable that is only valid in the current PS process. In your case, nothing is printed to the screen because the variable does not exist in the new PowerShell process.

The simplest method to get the example you provided working is to rework your quotes to allow for PowerShell substitution of the variable in the string. This basically passes the literal value to the process rather than asking it to evaluate a variable it knows nothing about.

Try this, Start-Process PowerShell "-command Echo $var; start-sleep -s 5" What gets passed to the sub process is: -command echo 10; start-sleep -s 5 $var is substituted before the call to start the process begins.

If your goal is to define variables in the other process then the best way is to use script blocks. Everything in the script block is executed in the remote process. Variables can be defined, functions, just about anything you can do locally, you can do in the child process. A script block defines code to be executed in the other process and can be called anything. In the example below, I purposely avoided calling it $ScriptBlock to demonstrate this. The typical naming convention is to just name the variable $ScriptBlock.

Example: I'm going to drop ECHO and use Write-Host. Either would work in this example. In addition, lets pass the -NoExit parameter to keep the window open rather than sleep. To pass multiple parameters to PowerShell, we'll use the -ArgumentList parameter.

$ScriptBlockExample = {$var = 10; Write-Host "This is my value" $var;}
Start-Process PowerShell -ArgumentList "-NoExit $scriptBlockExample"

This will open a new PS window and execute the contents of the script block. Once the command completes, type $var in the new window you'll receive the value 10 back. This is because the variable was defined in the new process.

The trick is to understand that by default, the two processes don't talk to each other. If you had $var defined in the parent process window, it would be completely independent of the $var in the second process. There are ways to get them to talk but, that's something for another day.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ernest Correale
  • 451
  • 3
  • 5