3

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?

jan-seins
  • 131
  • 1
  • 6
  • 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 '18 at 23:13
  • 1
    (get-process -id 17512).StartInfo.EnvironmentVariables < – Dallas Jan 18 '18 at 23:24
  • @Dallas no doubt because environment variables are not process specific or anything to do with processes! – barlop Dec 06 '22 at 07:00

2 Answers2

0

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

In one terminal

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

In another:

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

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

Carl Walsh
  • 393
  • 3
  • 16
  • since when has environment variables been process specific?! – barlop Dec 06 '22 at 06:59
  • 2
    @barlop each process will have different environment variables. See [the MSDN guide for Windows](https://learn.microsoft.com/en-us/windows/win32/shell/user-environment-variables) and a [how-to guide](https://www.makeuseof.com/how-to-use-environment-variables-in-windows-10/). – Carl Walsh Dec 06 '22 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 '22 at 09: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 '22 at 20:02
-2

Try the following in powershell:

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

OR

(Get-Process -id {PID here}).StartInfo.EnvironmentVariables
Art Gertner
  • 7,149
  • 11
  • 42
  • 72
  • 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 '17 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 '18 at 01:38
  • @VertigoRay maybe because there are two types of environment variables, user environment variables and system environment variables! – barlop Dec 06 '22 at 07:02
  • @jan-seins a)environment variables are not process specific, so this answer is misleading b)to have environment variables set permanently, the normal way is through the GUI of the OS. Another way is to use set them in the registry, with from cmd or from the GUI of the OS. And another way is to use the setx command, which sets them in the registry (though the setx command can be ugly, in the sense that one shud save the contents of an environmnt variable somewhere b4 using setx! Once u have done that then open a new cmd window, it will show. Setting in a cmd window with SET will be temporary – barlop Dec 06 '22 at 07:04
  • 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 06 '22 at 23:32
  • @CarlWalsh thanks.. interesting.. Is there a process that is the parent from which the environment variables set at the OS level in the GUI, are put with / associated with? eg a process that has the PATH variable, that any cmd.exe process would inherit? – barlop Dec 07 '22 at 03:30
  • 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](https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-settingchange) to all listening windows ([here's a blog post with details](https://www.angryadmin.co.uk/?tag=wm_settingchange)). 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 07 '22 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 '22 at 09:24
  • 1
    @barlop Cheers! It's been a while, but it was likely from scoping. Otherwise, it could have been from other environment variables being expanded at the time of the query. A good demonstration of process (or service) level environment variables can be seen here: https://serverfault.com/a/1103091/145894 – VertigoRay Dec 13 '22 at 05:08
  • @CarlWalsh Thanks.. You might have some ideas re https://superuser.com/questions/1764000/is-there-a-way-to-set-environment-variables-for-explorer-exe-that-arent-there-w – barlop Jan 20 '23 at 16:58