首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell:跟>吗?

Powershell:跟>吗?
EN

Stack Overflow用户
提问于 2017-04-26 08:06:10
回答 2查看 568关注 0票数 2

在PowerShell中,|>有什么区别?

代码语言:javascript
复制
dir | CLIP #move data to clipboard
dir > CLIP #not moving, creating file CLIP (no extension)

假设|将当前结果移动到管道中的下一个块,并且>将数据保存到文件中,这样做正确吗?

还有其他的区别吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-26 08:28:11

(不完全)是的。

|>是两种不同的东西。

>是一个所谓的重定向运算符。

重定向操作符将流的输出重定向到文件或其他流。管道运算符将cmdlet或funtion的返回对象连接到下一个(或管道的末尾)。当管道用它的特性来驱动整个对象时,重定向管道只是它的输出。我们可以用一个简单的例子来说明这一点:

代码语言:javascript
复制
#Get the first process in the process list and pipe it to `Set-Content`
PS> (Get-Process)[0] | Set-Content D:\test.test
PS> Get-Content D:/test.test

输出

System.Diagnostics.Process (AdAppMgrSvc)

尝试将对象转换为字符串。

代码语言:javascript
复制
#Do the same, but now redirect the (formatted) output to the file
PS> (Get-Process)[0] > D:\test.test
PS> Get-Content D:/test.test

输出

处理NPM(K) PM(K) WS(K) CPU Id SI ProcessName

第三个例子将展示管道操作符的功能:

代码语言:javascript
复制
PS> (Get-Process)[0] | select * | Set-Content D:\test.test
PS> Get-Content D:/test.test

这将输出一个包含所有进程属性的Hashtable:

@{en1;Threads=System.Diagnostics.ProcessThreadCollection;;PriorityClass=;FileVersion=;HandleCount=420;WorkingSet=9519104;PagedMemorySize=6045696;PrivateMemorySize=6045696;VirtualMemorySize=110989312;TotalProcessorTime=;SI=0;Handles=420;VM=110989312;WS=9519104;PM=6045696;VM=110989312;VM=110989312;PriorityClass=;PriorityClass=;PriorityClass=24;;#en32;#en33;#en37;#en40;#en41;#en42;;#en44;#en1;#;#en55;en53#en53#en38#en41 en42 en44 en45 en46 en47 en35 en35 en37 en41 en42 en1 44 en45#en1 46#en1 47#en58#47#en58 48#enen5;VirtualMemorySize64=110989312;EnableRaisingEvents=False;StandardInput=;StandardOutput=;StandardError=;WorkingSet64=9519104;Site=;Container=}

票数 5
EN

Stack Overflow用户

发布于 2017-04-26 08:14:35

你说得对:

  • |将对象输送到成功/输出流中

当您将对象从一个cmdlet输送到另一个cmdlet时,您不希望接收cmdlet接收错误、警告、调试消息或详细消息以及它设计用来处理的对象。 因此,管道运算符(X)实际上将对象输送到输出流(流#1)。

有关重定向的更多信息:Redirection

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43628341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档