Link to home
Start Free TrialLog in
Avatar of SnowFlake
SnowFlakeFlag for Israel

asked on

environment variables

If I have a cmd.exe window and a powershell window running both.
1. is there anything I can write in the powershell window to make changes to the environment variables as they are shown using the set command in the cmd window ?
2. the same if the posh window is executed from the cmd window

i.e.

from a .cmd file run posh with -Command calling upon some script such that when the next command in the .cmd file is executed %myVar% reflects a value set via the posh script.

SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SnowFlake

ASKER

Hi Chris,
Thanks for you answer,
I was already familiar with all of those ways to work with environment variables from inside POSH but non of them seem to do what I asked for.

try opening a command window (start-run cmd)
type in it set

Open in addition to the cmd window a posh window and try setting or modifying an environment variable in any of those windows.

I was not able to get a change made in one of those windows reflect in values shown the other one,
not event when I tried

[Environment]::SetEnvironmentVariable("testvar", "5", "Machine")

Each window seems to have its own set of variable that derive from the same original but don't seem to effect each other in any way.

Looks like I am missing some sort of flush or reload method.
BTW, It changes don't seem to "cross over" even to a second PoSh window.

Any ideas ?

Eyal

Okay, sorry I'm with you now.

This statement is completely correct:

> Each window seems to have its own set of variable that derive from the same original but don't
> seem to effect each other in any way.

They get the set from the system (user and system variables) at start up, they don't refresh the contents of those variables after startup. So a change in one session will not effect another I'm afraid.

Chris
but when calling a batch file from within command does allow you to change the variables in that "session" of command ...

I guess this means that the execution of the batch is done in the same proccesss while running posh is in a different process.

is there a way to force cmd.exe to reload the updated value of those variables ?

I want to focus on the scenario where cmd.exe runs posh and wants to return some result.

I guess I could create a batch from posh and run it from cmd after posh exits ...
but it just feels so lame ...

Indeed, cmd.exe parses the batch file, no new process is needed. But PowerShell must be started to execute a PS script.

I'm not familiar with a way to reload the variable set in cmd.exe, but I never really liked batch scripting so I'm not sure I would want to assert that it cannot be done. I realise that's not a very helpful response, sorry for that.

Chris
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Note for those who ant to use %errorlevel%
if you start POSH with the -Command parameter and run a script you have to end the script block passed to -Command with  exit $lastexitcode
otherwise cmd.exe will get the result of the script block which will be 0.

Thanks Everyone.