4

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
  • 141

2 Answers2

1

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.

-1

Try the following in powershell:

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

OR

(Get-Process -id {PID here}).StartInfo.EnvironmentVariables
Art Gertner
  • 7,429