3

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
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

2 Answers2

2

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.

Matt
  • 45,022
  • 8
  • 78
  • 119
  • You have a space in `Select-Window *Online *` remove the space like in your example `Select-Window *Online*` or `Select-Window *Online Video*`. It thinks the lone `*` asterisk is a separate argument which it is not, hence the error you got. – Matt Sep 14 '14 at 12:11
  • @Matt: thanks for sharing..could you please test if delayed bind scriptblock work for you: select-window...| where...| select -first 1 | get-process -pid { $_.processId} – walid toumi Sep 14 '14 at 12:14
  • @walidtoumi Yes that does work and keeps the code as a single line. Updated the answer with your comment. You should change your answer to your comment. That is the easiest answer I think. – Matt Sep 14 '14 at 12:19
  • @Matt: +1 for testing – walid toumi Sep 14 '14 at 12:21
  • Great. for those who wants ( to make a window- top most via a substring of its title) : http://jsbin.com/vewoco/2/edit notice. this codes searches only for dynamic names in the **chrome** process. ( chrome is not a part of params). I just dont know how to send 2 params :-)... ps all the remarks are remarks of older code which selects via process name and not via window – Royi Namir Sep 14 '14 at 12:22
  • ( for those who ask why ? - well it's nice to see video in a 300x300 chrome - while programming.) – Royi Namir Sep 14 '14 at 12:28
1

Try this if work:

....$WindowTitle*"} | get-process |Select-Object ..
walid toumi
  • 2,172
  • 1
  • 13
  • 10