Is there a way to view the environment variables of a specific process. I know that i can see them using the Process Explorer. However, i need to use them in a batch or powershell script. Is there a command that does that. Can I use the procexp.exe of the Process explorer to get this information to use it in a following script?

  • Not sure I can speak to "of a specific process" (session variables), but if you want to see all the environmental variables (just System and User I thought) you can use: (Get-WMIObject -Class "Win32_Environment")
    – Dallas
    Jan 18, 2018 at 23:13
  • 1
    (get-process -id 17512).StartInfo.EnvironmentVariables <<also just appears to pull the same System and User environmental variables
    – Dallas
    Jan 18, 2018 at 23:24
  • @Dallas no doubt because environment variables are not process specific or anything to do with processes!
    – barlop
    Dec 6, 2022 at 7:00

2 Answers 2


For Windows, this is messy but not complicated using Python psutil.Process.environ() library function.

In one terminal

$ $ENV:test = 'abc'
$ $PID

In another:

$ python -c 'import psutil; print(psutil.Process(pid=108444).environ()["TEST"])'

(Notice the variable names are capitalized in Windows.)

I tested this in macOS and linux (RHEL8), and it doesn't work. This psutil functionality works real-time in Windows, but it's not guaranteed for all OS. The documentation says this:

Note: this might not reflect changes made after the process started.

  • since when has environment variables been process specific?!
    – barlop
    Dec 6, 2022 at 6:59
  • 2
    @barlop each process will have different environment variables. See the MSDN guide for Windows and a how-to guide.
    – Carl Walsh
    Dec 6, 2022 at 23:21
  • From the doc: The environment variables of the process as a dict. Note: this might not reflect changes made after the process started. So this is just the same as StartInfo.EnvironmentVariables which is a snapshot at process start time
    – phuclv
    Dec 11, 2022 at 9:12
  • @phuclv I updated answer that it only works real-time in Windows. You can validate it with the same test I did; on Windows it's not just a snapshot of process start, but instead it's real-time.
    – Carl Walsh
    Dec 12, 2022 at 20:02

Try the following in powershell:

(Get-Process {Process image name here}).StartInfo.EnvironmentVariables


(Get-Process -id {PID here}).StartInfo.EnvironmentVariables
  • 2
    That does not work correctly. You can check this easily if you open a cmd and set a variable. The var is now set in the context of the cmd-process. However, if you run your ps-command you will not see the new variable. In the ProcessExplorer you can see it.
    – jan-seins
    Sep 22, 2017 at 11:40
  • 2
    Additionally, I've seen where this has very incorrect environment variables. For example, I start a process as User1 and User1 sees User1's environment variables as you would expect. If query the same process from User2, User2 sees User2's environment variables.
    – VertigoRay
    Aug 16, 2018 at 1:38
  • 3
    @barlop what you are saying is generally correct, but you are missing that each process can set the process-level environment variables, which are inherited by child processes. SET in a CMD window affects both the current CMD.exe process's variables, and the environment variables for any child processes created by CMD. The question is about seeing the current environment variables for some process, not the default environment variables for a new process created by explorer.
    – Carl Walsh
    Dec 6, 2022 at 23:32
  • 1
    @barlop yes exactly, for windows that parent is explorer.exe which is the GUI you click on. After updating i.e. the registry for the default system PATH env var, windows sends out the WM_SETTINGCHANGE message to all listening windows (here's a blog post with details). Most processes don't listen to that, but EXPLORER has the feature to update it's own env vars, and then any new process EXPLORER creates will have the new env vars.
    – Carl Walsh
    Dec 7, 2022 at 18:14
  • 1
    @CarlWalsh actually you can have a different shell instead of using the default explorer.exe. This is also common in WinPE environment
    – phuclv
    Dec 11, 2022 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.