1

I've created the following PowerShell script to kill a process:

$oProcs = Get-WmiObject Win32_Process -filter "commandline like '%G:\\TCAFiles\\Users\\Admin\\2155\\Unturned.exe%'";foreach ($oProc in $oProcs){Stop-Process $oProc.Handle}

The above script works fine, however when I'm trying to make the script start from Command Prompt it fails.

powershell -Mta -NoProfile -Command "$oProcs = Get-WmiObject Win32_Process -filter "commandline like '%G:\TCAFiles\Users\Admin\2155\Unturned.exe%'";foreach ($oProc in $oProcs){Stop-Process $oProc.Handle}"

This results in the following error:

Get-WmiObject : A positional parameter cannot be found that accepts argument
'%G:\TCAFiles\Users\Admin\2155\Unturned.ex e%'.
At line:1 char:11
+ $oProcs = Get-WmiObject Win32_Process -filter commandline like '%G:\T ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand

I'm not sure what this error means or how to resolve it.

2
  • 2
    Your issue is with unescaped double quotes. But it would be easier to use Get-Process | Where-Object {$_.Path -eq 'G:\TCAFiles\Users\Admin\ \2155\Unturned.exe'} | Stop-Process or just Get-Process Unturned | Stop-Process Commented Jul 17, 2017 at 22:03
  • 2
    You don't need to run it from cmd.exe. Run it from the PowerShell prompt instead. Commented Jul 17, 2017 at 22:05

1 Answer 1

2

You will need to escape any double quotes inside the powershell code that you are passing as an argument. At the moment your command argument will end after "-filter ".

If you are running this from cmd you can escape the double quotes using a backslash:

powershell -Mta -NoProfile -Command "$oProcs = Get-WmiObject Win32_Process -filter \"commandline like '%G:\TCAFiles\Users\Admin\2155\Unturned.exe%'\";foreach ($oProc in $oProcs){Stop-Process $oProc.Handle}"

Or if you are running this in powershell you can escape them by prepending it with a backtick or another double quote:

powershell -Mta -NoProfile -Command "$oProcs = Get-WmiObject Win32_Process -filter ""commandline like '%G:\TCAFiles\Users\Admin\2155\Unturned.exe%'"";foreach ($oProc in $oProcs){Stop-Process $oProc.Handle}"
Sign up to request clarification or add additional context in comments.

1 Comment

You're the best! First time I saw someone explain that there are different ways to escape the quotes in powershell and cmd. Me, I was just about to ask the same question, even though I tried to escape quotes in cmd ... but using the escape method from powershell :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.