3

I am trying to execute the following powershell commands from CMD, for example:

powershell Get-WmiObject Win32_PnPSignedDriver 

powershell Get-WmiObject Win32_PnPSignedDriver > test.txt 

which both work correctly.

But when I do a query, for example:

powershell (Get-WmiObject Win32_PnPSignedDriver | where {$_.location -like "*PCI bus 0, device 22, function 0*"}).DeviceName

I am getting this message the cause of which I can not pin down:

INFO: Could not find files for the given pattern(s)

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
Wojciech Szabowicz
  • 3,646
  • 5
  • 43
  • 87

3 Answers3

3

This seems to work for me (I was seeing the same error as you originally):

powershell -command "(Get-WmiObject Win32_PnPSignedDriver | where {$_.location -like '*PCI bus 0, device 22, function 0*'}).DeviceName"
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
1

To complement the existing answers with general guidelines for passing commands to powershell.exe from cmd.exe (a Command Prompt):

  • Enclose the entire PowerShell command in "..." (double quotes).

    • This protects its contents from unwanted up-front interpretation by cmd.exe - as happened with | in this case, as explained in aschipfl's answer.
    • However, cmd.exe-style environment-variable references (e.g., %USERNAME%) are still expanded.
  • For quoting that is embedded in the PowerShell command:

    • Use '...' (single quotes) where feasible; this is what Mark Wragg's helpful answer does, but it is only an option if the quoted string is meant to be a literal (doesn't contain variable references or subexpressions).

    • If you do need embedded "...", escape it as \"...\"

      • Note that while inside PowerShell it is the ` (backtick) that serves as the escape character, when passing strings from the outside PowerShell requires \.

Simplified examples (run from cmd.exe):

Note that passing a command without a parameter name implies the -Command parameter; run powershell -? to see the command-line syntax.
You may also want to use -NoProfile so that the user profile isn't loaded every time.

Commands that don't need embedded quoting - simply double-quote:

 powershell -noprofile "get-date"
 powershell -noprofile "(get-date).Date"
 powershell -noprofile "get-date | get-member"

Commands with embedded quoting - use '...' or \"...\":

 powershell -noprofile "(get-date).Year -like '*17'"
 powershell -noprofile "$v=17; (get-date).Year -like \"*$v\""

Commands that incorporate the value of a cmd.exe environment variable:

 :: # Nothing special to do: cmd.exe expands the reference irrespective of quoting.
 set "v=17"
 powershell -noprofile "(get-date).Year -like '*%v%'"

 :: # More robust variant that passes the value as an argument to a script block.
 set "v=17"
 powershell -noprofile ". { param($v) (get-date).Year -like \"*$v\" }" "%v%"

Optional reading: calling powershell from POSIX-like shells such as bash:

The above rules apply analogously, except that you'll typically use '...' (single quotes) to enclose the entire PowerShell command, which categorically prevents up-front interpretation by the shell.

Using "..." is an option if up-front expansions of shell-variable references and command substitutions are explicitly desired, but the potential for confusion is great, because both POSIX-like shells and PowerShell use sigil $ to refer to variables - it may not be obvious what is expanded when.

POSIX-like shells categorically do not support embedding ' instances in side '...' strings, which necessitates a somewhat awkward workaround (see below).

Simplified examples (run from a POSIX-like shell such as bash):

Commands that don't need embedded quoting - simply single-quote:

 powershell -noprofile 'get-date'
 powershell -noprofile '(get-date).Date'
 powershell -noprofile 'get-date | get-member'

Commands with embedded quoting - replace embedded ' instances with '\'' (sic) and use embedded " as-is:

 powershell -noprofile '(get-date).Year -like '\''*17'\'''
 powershell -noprofile '$v=17; (get-date).Year -like "*$v"'

Commands that incorporate the value of a shell/environment variable:

 # By using a double-quoted string, the shell expands $v up front.
 v=17
 powershell -noprofile "(get-date).Year -like '*$v'"

 # It gets trickier if you want to reference PS variables too.
 # Note how the PS variable's $ is \-escaped so that the shell doesn't interpret it.
 v=$HOME
 powershell -noprofile "\$HOME -eq '$v'"

 # More robust variant that passes the value as an argument to a script block.
 v=17
 powershell -noprofile '. { param($v) (get-date).Year -like "*$v" }' "$v"
Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

I am pretty sure that the pipe character | is the problem here, because cmd tries to process it. Simply escape it by preceding with ^:

powershell (Get-WmiObject Win32_PnPSignedDriver ^| where {$_.location -like "*PCI bus 0, device 22, function 0*"}).DeviceName

If this code is used within a parenthesised block of code in cmd, you may need to escape the closing ) as well (the opening ( can be escaped too, but there is no need):

powershell ^(Get-WmiObject Win32_PnPSignedDriver ^| where {$_.location -like "*PCI bus 0, device 22, function 0*"}^).DeviceName
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 1
    The `|` is one problem, but there is another: the `"` chars. are removed and must therefore be escaped as `\"`. – mklement0 Apr 19 '17 at 19:35