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
Walid's answer is valid but it will return object with a processes matching
ProcessName
. You are looking for a match with a singleprocessId
If you don't need the other data you could just select the ProcessID
From your code snippet:
Just put that code right in a Get-Process call.
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 thisThis 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
Always though that would could only use
$_
in something like a foreach. Thanks for the tip.