A7ec
A7ec

Reputation: 146

how to do taskkill for all programs in the folder using cmd / powershell

I want to do Taskkill for all programs ending with exe in a folder with cmd / powershell

Example

taskkill /f /im C:\folder\*.exe

Upvotes: 0

Views: 2650

Answers (3)

mklement0
mklement0

Reputation: 437618

nimizen's helpful PowerShell answer is effective, but can be simplified:

Get-Process | 
  Where-Object { (Split-Path -Parent $_.Path) -eq 'C:\folder' } |
    Stop-Process -WhatIf

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

Note:

  • The above only targets processes whose executables are located directly in C:\folder, by checking whether the (immediate) parent path (enclosing directory) is the one of interest, using Split-Path -Parent.

  • If you wanted to target executables located in C:\Folder and any of its subfolders, recursively, use the following (inside Where-Object's script block ({ ... }):

    $_.Path -like 'C:\folder\*'
    
  • Unlike Compo's helpful wmic.exe-based answer[1], this answer's solution also works on Unix-like platforms (using PowerShell (Core) 7+).


[1] Technically, wmic.exe is deprecated, as evidenced by wmic /? printing WMIC is deprecated in red, as the first (nonempty) line. Consider using PowerShell's CIM cmdlets, such as Get-CimInstance, instead, which has the added advantage of returning objects rather than text, for robust subsequent processing.

Upvotes: 1

nimizen
nimizen

Reputation: 3419

In PowerShell, something like this:

$files = gci "C:\Path\To\Files" -Filter "*.exe"

foreach($file in $files){
    Get-Process | 
    Where-Object {$_.Path -eq $file.FullName} | 
    Stop-Process -WhatIf
}

Remove the -WhatIf when you're confident that the correct processes would be stopped.

Upvotes: 2

Compo
Compo

Reputation: 38613

I would advise that you try a different built-in command utility, WMIC.exe:

%SystemRoot%\System32\wbem\WMIC.exe Process Where "ExecutablePath Like 'P:\\athTo\\Folder\\%'" Call Terminate 2>NUL

Just change P:\\athTo\\Folder as needed, remembering that each backward slash requires doubling. You may have difficulties with other characters in your 'folder' name, but those are outside of the scope of my answer. To learn more about those please read, LIKE Operator

Note: If you are running the command from a , as opposed to directly within , then change the % character to %%

Upvotes: 2

Related Questions