View process environment variables in Windows

1

1

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

Posted 2017-09-22T11:19:53.267

Reputation: 111

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 – 2018-01-18T23:13:45.317

(get-process -id 17512).StartInfo.EnvironmentVariables <<also just appears to pull the same System and User environmental variables – Dallas – 2018-01-18T23:24:42.673

Answers

0

Try the following in powershell:

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

OR

(Get-Process -id {PID here}).StartInfo.EnvironmentVariables

Art Gertner

Posted 2017-09-22T11:19:53.267

Reputation: 6 417

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 – 2017-09-22T11:40:11.093

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 – 2018-08-16T01:38:31.840