• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 946
  • Last Modified:

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.

0
SnowFlake
Asked:
SnowFlake
  • 3
  • 3
2 Solutions
 
Chris DentPowerShell DeveloperCommented:

1. Yes.

Environmental variables can be seen using the Environment provider, exposed as the Env "drive". This can be seen using:

Get-PsProvider

With that you can do:

cd Env:
dir

Where cd is an alias for Set-Location, and dir is an alias for Get-ChildItem. Therefore you can also do:

Get-ChildItem Env:

Individual entries are accessible as follows:

$Env:Path
$Env:ComputerName

To add a new environmental variable to the current PS session you can use:

$Env:SomeNewVariable = "SomeNewValue"

So far so good, however, variables created here are not permanent (although you can modify existing variables this way). If you need to add permanent variables you need .NET classes. Like this:

[Environment]::SetEnvironmentVariable("SomeNewVariable", "SomeNewValue", "Scope")

Where Scope is one of Process (same as current PS Session), Machine, or User.

2. Depends where you're using the variable. If PS is using it, then you'd use the $Env provider (e.g. $Env:myVar). If the Command Shell is dealing with the variable before executing anything in PowerShell you'd stick with %myVar%.

HTH

Chris
0
 
SnowFlakeAuthor Commented:
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
0
 
Chris DentPowerShell DeveloperCommented:

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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
SnowFlakeAuthor Commented:
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 ...
0
 
Chris DentPowerShell DeveloperCommented:

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
0
 
Shift-3Commented:
You can use the Exit command with a numerical argument to end a PowerShell script.  This is then accessible with the %errorlevel% variable in the batch script which called PowerShell.
0
 
SnowFlakeAuthor Commented:
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now