2

I use powershell to set a number of system environment variables, including the path. I then start some task with Start-Process. Unfortunately the new process has the same environment variables as those which powershell was started in. To reproduce the issue

1) Modify your path. I'm deleting %JAVA_HOME%/bin, open a command window and see that the java command does not work.

2) Start Powershell ISE

3) Add %JAVA_HOME%/bin to the system PATH. Open a new command window and see that the java command works.

4) In the previously opened Powershell window, submit the command start cmd to cause powershell to open a cmd.exe command window ("Command Prompt"). In the new command window verify that java does not work.

My expectation would be that a newly launched process would have its own fresh copy of the environment variables.

Can you tell me how to get powershell to launch a new task and have that task know the current environment?

I've tried

start cmd Start-Process -FilePath "cmd" Start-Process -FilePath "cmd" -UseNewEnvironment & cmd 

Thanks!

1
  • 1
    The -UseNewEnvironment switch for Start-Process is designed to do what you want, but it is (still) broken as of PowerShell Core 7.0.0-rc.2 - see this GitHub issue. Commented Jan 21, 2020 at 22:12

1 Answer 1

3

The following command will start a process which is detached from the calling environment and have the new variable available for you to use.

Invoke-CimMethod -ClassName Win32_Process -Name Create -Arguments @{CommandLine = "cmd.exe"}

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.