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?
Asked
Active
Viewed 9,256 times
2 Answers
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.
Carl Walsh
- 519
-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