Home > Software engineering >  how to do taskkill for all programs in the folder using cmd / powershell
how to do taskkill for all programs in the folder using cmd / powershell

Time:11-22

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

Example

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

CodePudding user response:

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 %%

CodePudding user response:

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.

CodePudding user response:

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\*'
    
  • Related