在CMD中运行带有管道的PowerShell命令

8
当我们在PowerShell中运行以下命令时:
get-date | sc C:\temp\date.log

它会创建一个名为date.log的文件,并使用当前日期进行命名。

但是,如果我们通过CMD运行相同的命令:

powershell get-date | sc C:\temp\date.log

它抱怨道:

ERROR: Unrecognized command

DESCRIPTION:
        SC is a command line program used for communicating with the
        Service Control Manager and services.
USAGE:
        sc <server> [command] [service name] <option1> <option2>...

显然,CMD混淆了为POSH准备的管道和自己的管道。

有人能指点我如何通过CMD运行它吗?

提前感谢。

2个回答

17

这里有一个损坏的链接。 - Marc
https://web.archive.org/web/20140206160933/http://technet.microsoft.com/en-us/library/hh847736.aspx - juanvan

0
只要使用双引号从 cmd 中转义管道符,它就可以工作。Sc 是一个不幸的别名,因为在 Windows 中有一个 sc.exe 命令。使用 ? 或 where-object 可能比尝试在查询中嵌入额外一组双引号更容易。
powershell "get-date | set-content c:\tmp\date.log"

powershell "get-ciminstance win32_volume | ? label -like 'month *' | select label"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接