Search code examples
powershellparametersprocessterminate

Escaping args[0] in Powershell


I'm writing a script to terminate a process identified by it's Commandline call. It works fine if i hardcode it like:

Get-WmiObject Win32_Process -Filter "CommandLine LIKE '%worker04%'" | Invoke-WmiMethod -Name Terminate

Now I want to work with a parameter like this:

Get-WmiObject Win32_Process -Filter "CommandLine LIKE '%$args[0]%'" | Invoke-WmiMethod -Name Terminate

so I can call my script like this:

.\killprocess worker04

So far it does nothing. How do I correctly put the $args[0] in the -Filter block?


Solution

  • I think this will do it:

    Get-WmiObject Win32_Process -Filter "CommandLine LIKE '%$($args[0])%'" | Invoke-WmiMethod -Name Terminate