从 cmd 运行 powershell 命令

Qas*_*oud 51 windows powershell command-line process cmd.exe

我如何从 cmd 运行此命令:

powershell.exe "(get-process | ? {$_.Description -eq "Sysinter Process Explorer"}) | select processname | out-file $env:APPDATA\example.txt"

我仍然收到此错误:

您必须在“-eq”运算符的右侧提供一个值表达式。在 line:1 char:37 + (get-process | ? {$_.Description -eq <<<< Sysinternals Process Explorer}) | 选择进程名| 输出文件 $env:APPDATA\example.txt + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException +fullyQualifiedErrorId : ExpectedValueExpression

Sim*_*onS 72

powershell -command "get-process | ? {$_.Description -eq 'Sysinter Process Explorer'} | select processname | out-file $env:APPDATA\example.txt"
Run Code Online (Sandbox Code Playgroud)

基本上你有一个 powershell 命令并将它粘贴在这些引号之间以从 CMD 调用它

powershell -command " #PasteCodeHere "

在这些引号中,您必须使用,'否则会中断您的命令参数。

编辑:附加信息:

你经常会遇到这样的情况: powershell -command "& 'somestuff'"

&用于调用文件。当你只使用一个命令&是不必要的,当你想调用一个脚本时,你应该使用它。

powershell -command "& 'C:\foobar.ps1'"

你也可以powershell -file C:\file.ps1用来调用脚本

  • 如果要执行的命令有引号,则它们必须加倍。示例:powershell -command "dir ""c:\Program Files""" (3认同)
  • @myobis 实际上,将引号加倍是行不通的(在 Windows 10 cmd 中)。但是使用反斜杠转义确实:`powershell -command "dir \"c:\Program Files\" "` (3认同)