Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Process object by window in WASP/powershell?

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
like image 564
Royi Namir Avatar asked Jun 15 '26 07:06

Royi Namir


2 Answers

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.

like image 109
Matt Avatar answered Jun 17 '26 03:06

Matt


Try this if work:

....$WindowTitle*"} | get-process |Select-Object ..
like image 39
walid toumi Avatar answered Jun 17 '26 02:06

walid toumi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!