Get Process object by window in WASP/powershell?

4.3k Views Asked by At

I have a function which makes a window -a TOPMOST window.

So - I can run this :

Get-WindowByProcessTitle *chrome* | Set-TopMost

Notice the argument here is a process name ( "chrome" is in "chrome.exe" which is the process).

The inner code which finally selects the process is :

Get-Process |  Where-Object {$_.MainWindowTitle -like "*chrome*"} | Select-Object Id,Name,MainWindowHandle,MainWindowTitle

Great.

Question

Now I have a query which select a window according to its title :

Select-Window *chrome* |  Where {$_.Title  -like "*$WindowTitle*"} | Select-Object -first 1  

Which yields :

ProcessName : chrome
ProcessId   : 3972
IsActive    : False
Handle      : 1641684
Title       : Watch Full movie The Beach (2000) Online Free | FFilms.org - Google Chrome
Class       : Chrome_WidgetWin_1

How can I get the process object ( not the ProcessId) from this query of mine ?

I think I need something like : (psuedo)

Select-Window *chrome* |  Where {$_.Title  -like "*$WindowTitle*"} |What_Is_MyProcess_Object? |Select-Object Id,Name,MainWindowHandle,MainWindowTitle
2

There are 2 best solutions below

6
On BEST ANSWER

Walid's answer is valid but it will return object with a processes matching ProcessName. You are looking for a match with a single processId

If you don't need the other data you could just select the ProcessID

From your code snippet:

Select-Window *Online video* |  Where {$_.Title  -like "*$WindowTitle*"} | Select-Object -first 1  

Just put that code right in a Get-Process call.

Get-Process -Pid (Select-Window *Online video* |  Where {$_.Title  -like "*$WindowTitle*"} | Select-Object -first 1 ).ProcessID

This will expand the process ID from your code and put it into the -pid of a get-process cmdlet. A more elegant solution might be this

$processToLocate = Select-Window *Online video* |  Where {$_.Title  -like "*$WindowTitle*"} | Select-Object -first 1 -ExpandProperty ProcessID
Get-Process -Pid $processToLocate

This should do the same thing. Just might be easier to read. Just uses -ExpandProperty instead of (Object).Property. The same goal is accomplished in both cases.

Or

Walid's suggestion from the comments would also work

Select-Window *Online video* |  Where {$_.Title  -like "*$WindowTitle*"} | Select-Object -first 1  | Get-Process -PID {$_.ProcessID}

Always though that would could only use $_ in something like a foreach. Thanks for the tip.

0
On

Try this if work:

....$WindowTitle*"} | get-process |Select-Object ..