HendrikK
HendrikK

Reputation: 56

Closing specific Child Process with Powershell

I want to close a specific child process via powershell based on the window title. Specifically it's about closing a specific chrome window. As I open the window via a powershell command, I do have the window title. But there are always multiple windows of chrome open. In the task manger it's also possible to close a specific child process via "right click".

enter image description here

At the end, I want to do the same but as a powershell command...

Any ideas? Help appreciated.

I'm already able to get all chrome processes, which are running. But only the last active chrome process/window has a mainWindowTitle given.

enter image description here

I also had the idea to just get the window I want to close back in foreground and then just send the shortcut keys to close the window. But this also isn't that easy as I don't get the process/window based on the window title in access to some further operations, when it's not the last active.

Upvotes: 0

Views: 186

Answers (1)

Clijsters
Clijsters

Reputation: 4256

Closing Child Processes using their window title is quite easy, but before I explain how to do this, I'll explain what I think you are really looking for.

When you start a Child Process, you usually use Start-Process, e.g.:

Start-Process chrome.exe "https://ober-geschaeftsfuehrer.de"

The cmdlet returns nothing and it would be your challenge to find that process and identify it (for example using MainWindowTitle, like you tried before)

But, what if I told you, that you can just assign a variable with the created process and manipulate this process using your variable value?

By using -PassThru, a Parameter commonly available on some CmdLets, the cmdlet will return the object(s) affected by its execution. In that specific case it returns the created process handle, which you will address later for killing that process:

$myProcess = Start-Process chrome.exe "https://ober-geschaeftsfuehrer.de" -PassThru
# Doing stuff or waiting for user interaction
Stop-Process $myProcess.Id

Upvotes: 1

Related Questions