Powershell set environment variable & use RRS feed

  • Question

  • I have a script that sets a user environment variable on logon and then I want to use that env in subsequent code but it doesn't immediately recognise the env. It only recognises after manually closing the env script. I have tried various ways of kicking of the code in a fresh cmd or Powershell session but it doesn't recognise. This is last stage I was trying:

    First CMD that runs:

    Start D:\Logon\logonSetEnv.cmd
    Start D:\Logon\logonOutlook.cmd

    logonSetEnv cmd:

    PowerShell -NoProfile -ExecutionPolicy RemoteSigned "D:\Logon\EmailAddress_SetEnv.ps1"
    Taskkill /f /im powershell.exe
    Taskkill /f /im cmd.exe

    ENV code:

    param ()

    Import-Module ActiveDirectory
    # Delete Environment Variable
    [System.Environment]::SetEnvironmentVariable("EmailAddress", $null, 'User')
    # Get email address associated with the username
    $EmailAddress = (Get-ADUser $env:USERNAME -Properties mail).mail
    # Create a user based environment variable called email address
    [System.Environment]::SetEnvironmentVariable("EmailAddress", $EmailAddress, 'User')


    PowerShell -NoProfile -ExecutionPolicy RemoteSigned "D:\Logon\Outlook.ps1"

    I also tried to initiate Outlook.ps1 from within the ENV code = Start-Process Powershell.exe '& D:\Logon\Outlook.ps1' but same result.

    Anyone have a suggestion to get script to set environment variable and that env to be recognised in subsequent script that follows?


    • Moved by Bill_Stewart Monday, September 11, 2017 3:25 PM This is not "teach me basics of how environment variable inheritance works" forum
    Friday, July 7, 2017 5:55 AM

All replies

  • You cannot externally change the environment of a running script or program.  The changes will only affect new programs. THis is a basic WIndows behavior and has always been the way Windows works.


    Friday, July 7, 2017 6:02 AM
  • So how can I initiate a fresh new script session within a script so changes will be seen?
    Friday, July 7, 2017 6:29 AM
  • if you use [System.Environment]::SetEnvironmentVariable  you set environment variables in registry. So all new proceses inheritance these variables but old processes continue to use old variables. But you can set environmen variables not only at the root (in the registry) but for the current process:


    All processes that will be started from this process will inheritance environment variables of parent.

    my blog: http://shserg.ru/

    • Edited by s.h.s. _ Friday, July 7, 2017 7:39 AM
    Friday, July 7, 2017 7:37 AM
  • To expand:


    We can also just use the string for the target:


    "Process" is immediate and effects current process an all child processes.

    "User" sets this in the users registry and "Machine" sets th system environment for all new processes or for processes that subscribe to changes in environment settings.

    CMD.EXE can do this with the SETX.EXE utility.


    • Edited by jrv Friday, July 7, 2017 8:06 AM
    Friday, July 7, 2017 8:05 AM
  • The reason I am trying to set an environment variable is so the outlook prf file can recognise it. PRF files will not recognise a Powershell variable as far as I can tell.
    Friday, July 7, 2017 8:28 AM
  • PRF files cannot use variables of any kind.  It is Outlook that has the variables.  Outlook does  not use environment variables.  It uses registry settings and configuration files.

    A PRF file is used for first time configuration of Outlook and can be applied at runtime from the command line.

    outlook.exe /importprf \\server1\share\outlook.prf

    No variables here.


    Friday, July 7, 2017 8:33 AM
  • Sounds like I need to set in registry and then somehow reference this path in PRF
    Friday, July 7, 2017 8:42 AM
  • I think you need to learn how Outlook and PRF files work.  They do not use the registry.  Outlook uses the registry.

    In a domain it is possible to get a PRF to obtain the users email from AD. It is not something we script.

    A PRF will read environment variables.  TO get the mailbox we use this section:


    Note that the mailbox is assigned using the %username% variable.  This will get all email addresses associated with the mailbox.

    Post in Exchange forum to learn how to set up Outlook with Exchange.


    Friday, July 7, 2017 8:53 AM
  • Yes sure but %username% doesn't resolve to the users primary SMTP address that Outlook recognises
    Friday, July 7, 2017 9:04 AM
  • Yes sure but %username% doesn't resolve to the users primary SMTP address that Outlook recognises

    No - it is used to get the mailbox and that has the email address.

    Why do you think you need the address.  It is not required to connect a users mailbox.


    Friday, July 7, 2017 9:09 AM
  • I managed to automate User environment variable being set but Outlook PRF doesn't recognise User env but it does System env.
    Monday, July 10, 2017 10:01 AM
  • I managed to automate User environment variable being set but Outlook PRF doesn't recognise User env but it does System env.

    PRF see both user and system environment.  You must set the user environment in the same process that executes the PRF in a GPO or it may not get set before the PRF is used.

    You would find more instructions on how to correctly use a PRF if you post in the Outlook deployment forum or the Exchange forum.


    • Edited by jrv Monday, July 10, 2017 10:31 AM
    Monday, July 10, 2017 10:30 AM