windows 如何在 PowerShell 中使用 cmd 类型管道 (/piping)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5317269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use cmd type pipe (/piping) in PowerShell?
提问by martind
In cmd (and bash), pipe "|
" pushes output to another command in the original format of the first command's output (as string).
在 cmd(和 bash)中,管道“ |
”以第一个命令输出的原始格式(作为字符串)将输出推送到另一个命令。
In PowerShell, everything that comes out the pipe is an object (even a string is a string object).
在 PowerShell 中,从管道中出来的所有东西都是一个对象(甚至一个字符串也是一个字符串对象)。
Because of that, some commands fail when run in a PowerShell command window as opposed to a Windows command window.
因此,某些命令在 PowerShell 命令窗口而不是 Windows 命令窗口中运行时会失败。
Example:
例子:
dir c:\windows | gzip > test.gz
When this command is run in the Windows command prompt window it works properly - directory listing of C:\windows gets compressed into test.gz file.
当此命令在 Windows 命令提示符窗口中运行时,它可以正常工作 - C:\windows 的目录列表被压缩到 test.gz 文件中。
The same command in PowerShell fails, because PowerShell does not use cmd-style pipe and replaces it with PowerShell pipe (working with array of file system items).
PowerShell 中的相同命令失败,因为 PowerShell 不使用 cmd 样式的管道,而是将其替换为 PowerShell 管道(使用文件系统项数组)。
Q.How do you disable the default piping behavior in PowerShell to make traditional Windows commands work identically in PowerShell?
问:如何禁用 PowerShell 中的默认管道行为,以使传统的 Windows 命令在 PowerShell 中以相同的方式工作?
I tried using the escape character "`
" before the pipe "`|
", but it didn't work. I also tried invoke-expression -command "command with | here"
, but it also failed.
我尝试`
在管道“ `|
”之前使用转义字符“ ”,但没有用。我也试过invoke-expression -command "command with | here"
,但也失败了。
回答by Andy Schneider
if you want to send strings down the pipeline you can use the cmdlet "out-string"
如果要沿管道发送字符串,可以使用 cmdlet“out-string”
For Example:
例如:
get-process | out-string
If you are specifically looking for a PowerShell way to zip up files, check out the PowerShell Community Extensions. there are a bunch of cmdlets to zip and unzip all kinds of files.
如果您专门寻找一种 PowerShell 方式来压缩文件,请查看 PowerShell 社区扩展。有一堆 cmdlet 可以压缩和解压缩各种文件。
回答by OldFart
If you can pipe the output of (CMD) dir into gzip, then gzip apparently knows how to parse dir output. The (string) output from the PowerShell dir command (aka Get-ChildItem) doesn't look the same, so gzip likely would not be able to parse it. But, I'd also guess that gzip would be happy to take a list of paths, so this would probably work:
如果您可以将 (CMD) dir 的输出通过管道传输到 gzip,那么 gzip 显然知道如何解析 dir 输出。PowerShell dir 命令(又名 Get-ChildItem)的(字符串)输出看起来不一样,因此 gzip 可能无法解析它。但是,我也猜想 gzip 会很乐意接受路径列表,所以这可能会起作用:
dir c:\windows | select -ExpandProperty FullName | gzip > test.gz
No warrantees express or implied.
没有任何明示或暗示的保证。
回答by TallGuy
If you really need to use the old school DOS pipe system in PowerShell, it can be done by running a command in a separate, temporary DOS session:
如果你真的需要在 PowerShell 中使用老式的 DOS 管道系统,可以通过在单独的临时 DOS 会话中运行命令来完成:
& cmd /c "dir c:\windows | gzip > test.gz"
The /c
switch tells cmd
to run the command then exit. Of course, this only works if all the commands are old school DOS - you can't mix-n-match them with PowerShell commands.
该/c
开关告诉cmd
运行的命令,然后退出。当然,这只有在所有命令都是老式 DOS 时才有效——你不能将它们与 PowerShell 命令混合匹配。
While there are PowerShell alternatives to the example given in the question, there are lots of DOS programs that use the old pipe system and will not work in PowerShell. svnadmin load
is one that I've the pleasure of having to deal with.
虽然问题中给出的示例有 PowerShell 替代方案,但有很多 DOS 程序使用旧的管道系统并且无法在 PowerShell 中运行。svnadmin load
是我不得不处理的一种乐趣。
回答by Aaron Jensen
You can't. PowerShell was designed to pass objects down a pipeline, not text. There isn't a backwards-compatability mode to DOS.
你不能。PowerShell 旨在通过管道传递对象,而不是文本。DOS 没有向后兼容模式。