I started "Explorer.exe" from Powershell and want to get the process id of the explorer window so that I would not mis-operate on other explorer windows.
Code: Start-Process "Explorer.exe" -PassThru Result: I can see the process id but it's different with the real process id of the window in UISpy or in task manager. Seems explorer.exe start another process (B) can exit itself and finally we see the process (B). And the process I got is the exited process. Question: How can I get the real process id (B)?
Explorer.exe will momentarily start a brand new process, but that process will die quickly, handing off its state to an existing explorer process. In general, it will not persist and add to a growing collection of processes, as most programs would.
See this in action:
"Old explorer.exe instances"
Get-Process explorer
Start-Process explorer.exe
sleep 1 # wait for this one to die
"New explorer.exe instances"
Get-Process explorer
You will see that it's the same set of explorer instances. I think this is related to the reg key HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced [SeparateProcess]
来源:https://stackoverflow.com/questions/12084221/how-to-get-the-process-id-of-explorer-launched-from-powershell