Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save janisblaus/31acf064744c4ff029cc23e5c2a0a54b to your computer and use it in GitHub Desktop.
Save janisblaus/31acf064744c4ff029cc23e5c2a0a54b to your computer and use it in GitHub Desktop.
const { exec } = require('child_process');
const desiredArgument = 'your_argument';
exec(`wmic process where "CommandLine like '%%%${desiredArgument}%%%' and not CommandLine like '%%wmic%%'" get ProcessId`, (error, stdout) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
const processIds = stdout.split('\r\r\n').filter(line => line.trim() !== 'ProcessId');
processIds.forEach(processId => {
exec(`taskkill /PID ${processId} /F`, (error, stdout) => {
if (error) {
console.error(`Error killing process with ID ${processId}: ${error.message}`);
} else {
console.log(`Process with ID ${processId} terminated successfully.`);
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment