user4746612
user4746612

Reputation:

Powershell close multiple windows / end processes based on name

I have a pretty neat mess of batch/python scripts that install a program called MATRIS, followed by about 15 exe updates. Around 11 of these updates open a window telling me the that the update was successful.


Now it would be really fun to run a batch or powershell script which closes all of these windows for me. The last thing I tried was Get-Process | Where-Object {$_.Path -like "MatrisInstaller.APCIPLUS"} | Stop-Process -WhatIf I wasn't sure if it was the name as read in task manager, or like the title of the window, but I tried both. Capture of the window and task manager

Please note that a couple of these are (32 bit) - I'm not sure if that would impact the script.

I was able to run tasklist followed by kill {PID} but PIDs change: I'm not sure how to script it.

Please reply if you need any clarification, I've historically been poor at wording my questions.

Upvotes: 1

Views: 3920

Answers (2)

boxdog
boxdog

Reputation: 8432

In your example, Path is pointing to the executable file on disk, so while possible to use (if it is consistent), it won't match the name you find in the processes tab of Task Manager. Typically, people will use the name as shown on the Details tab of Task manager. For example, with Outlook on my system, these three possibilities are:

Path: C:\Program Files\Microsoft Office\Office16\OUTLOOK.EXE
Processes tab: Microsoft Outlook
Details tab: outlook.exe

So, you need a command like this:

Get-Process | Where Name -eq 'Outlook' | Stop-Process

or, better:

Get-Process -Name 'Outlook' | Stop-Process

Note that PowerShell expects you to remove the '.exe' you see in Task manager.

EDIT: Additional technique

If you know the names of the processes, then you can simplify your script by doing something like this:

$processList = "Process1","Process2","Process3" # Add all process names to the list

$processList |
    ForEach-Object {
        Get-Process -Name $_ | Stop-Process
    }

Upvotes: 3

Owain Esau
Owain Esau

Reputation: 1922

You were almost there, just need to change "Path" to "ProcessName" like so:

Get-Process | Where-Object {$_.ProcessName -like "MatrisInstaller.APCIPLUS"} | Stop-Process -WhatIf

Upvotes: 0

Related Questions