environment variables

Posted on 2009-12-30
Last Modified: 2012-05-08
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


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.

Question by:SnowFlake
    LVL 70

    Assisted Solution

    by:Chris Dent

    1. Yes.

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


    With that you can do:

    cd Env:

    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:


    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%.


    LVL 15

    Author Comment

    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 ?

    LVL 70

    Expert Comment

    by:Chris Dent

    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.

    LVL 15

    Author Comment

    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 ...
    LVL 70

    Expert Comment

    by:Chris Dent

    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.

    LVL 38

    Accepted Solution

    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.
    LVL 15

    Author Closing Comment

    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.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Formatting with Powershell 9 37
    EXCHANGE 2007, SCRIPTS 67 60
    powershell hanging 4 38
    Installed softare without GUID 6 30
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Synchronize a new Active Directory domain with an existing Office 365 tenant
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now