Search code examples
windowspowershellcmdcommandwildcard

Powershell - Running Command on CMD - Wildcards Throw Error


I am trying to run the following Powershell command via CMD:

powershell -command "Get-WmiObject Win32_Process | Where-Object {$_.CommandLine -like \"*C:\Windows\Test*\" } | Select-Object ProcessName, CommandLine"

The command above runs fine directly on Powershell, but only causes problems when I try to run it on CMD. In my testing, I discovered that the * symbol fails to process correctly, I tried to put a backslash before the symbol for testing and that has not done the trick. Is there a way to get this to work with the * symbol in CMD?

Edit: This command is used to view processes that contain a command-line of C:\Windows\Test


Solution

  • Simpler still, no need for PowerShell:

    wmic process where "commandline like '%c:\\windows\\test%'" get name, commandline
    

    And just for completeness, to remain properly on topic, using PowerShell from cmd.exe, I'd do it more like this:

    powershell -noprofile "get-ciminstance -query \"select * from win32_process where commandline like '%c:\\windows\\test%'\" | select-object -property processname, commandline"