如何从命令行运行管道 powershell 命令?

Tim*_*son 2 powershell command-line datetime file

我正在尝试从 Windows 7 命令行运行以下 powershell 命令:

powershell ls 'C:/path to file/' | ForEach-Object {$_.LastWriteTime=Get-Date}
Run Code Online (Sandbox Code Playgroud)

我遇到了几个错误。当我运行上述命令时,出现错误:

'ForEach-Object' is not recognized as an internal or external command, 
operable program or batch file.
Run Code Online (Sandbox Code Playgroud)

我将命令更改为:

powershell ls 'C:/My Programs/CPU Analysis/data/test/' | powershell ForEach-Object {$_.LastWriteTime=Get-Date}
Run Code Online (Sandbox Code Playgroud)

现在我收到错误:

Property 'LastWriteTime' cannot be found on this object; make sure it exists
and is settable.
At line:1 char:17
+ ForEach-Object {$_.LastWriteTime=Get-Date}
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
Run Code Online (Sandbox Code Playgroud)

如何修改此命令以从命令行运行?

更新

两种解决方案基本上都在说同样的事情,但@Trevor Sullivan 有一个更明确的答案。

小智 5

cmd.exe 不理解 foreach 对象。另外,您尝试将执行拆分到两个单独的 PowerShell 进程,这在这种情况下不起作用。

您需要在 PowerShell 中运行整个命令。

powershell "ls 'C:/My Programs/CPU Analysis/data/test/' | ForEach-Object {$_.LastWriteTime = Get-Date}"
Run Code Online (Sandbox Code Playgroud)