2017-04-26 67 views
2

在PowerShell中,|>之间有什么区别?Powershell:|之间的区别和>?

dir | CLIP #move data to clipboard 
dir > CLIP #not moving, creating file CLIP (no extension) 

我说得对假设|当前结果移动到下一个块的管道和>将数据保存到一个文件?

还有其他区别吗?

+2

重定向操作符'>'也将输出到一个字符串,而管'|'保持对象作为是 –

回答

5

(不完全)是的。

|>是两个不同的东西。

>是一个所谓的重定向操作符。

重定向操作符将流的输出重定向到文件或其他流。管道操作员将cmdlet或函数的返回对象传递给下一个(或管道的末端)。当管道用其属性抽取整个对象时,重定向管道只输出它的输出。我们可以用一个简单的例子说明这一点:

#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)

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


#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 

输出

Handles NPM(K) PM(K)  WS(K)  CPU(s)  Id SI ProcessName 
------- ------ -----  -----  ------  -- -- ----------- 
    420  25  6200  7512    3536 0 AdAppMgrSvc 

第三个例子将显示管操作者的能力:

PS> (Get-Process)[0] | select * | Set-Content D:\test.test 
PS> Get-Content D:/test.test 

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

@{Name=AdAppMgrSvc; Id=3536; PriorityClass=; FileVersion=; HandleCount=420; WorkingSet=9519104; PagedMemorySize=6045696; PrivateMemorySize=6045696; VirtualMemorySize=110989312; TotalProcessorTime=; SI=0; Handles=420; VM=110989312; WS=9519104; PM=6045696; NPM=25128; Path=; Company=; CPU=; ProductVersion=; Description=; Product=; __NounName=Process; BasePriority=8; ExitCode=; HasExited=; ExitTime=; Handle=; SafeHandle=; MachineName=.; MainWindowHandle=0; MainWindowTitle=; MainModule=; MaxWorkingSet=; MinWorkingSet=; Modules=; NonpagedSystemMemorySize=25128; NonpagedSystemMemorySize64=25128; PagedMemorySize64=6045696; PagedSystemMemorySize=236160; PagedSystemMemorySize64=236160; PeakPagedMemorySize=7028736; PeakPagedMemorySize64=7028736; PeakWorkingSet=19673088; PeakWorkingSet64=19673088; PeakVirtualMemorySize=135786496; PeakVirtualMemorySize64=135786496; PriorityBoostEnabled=; PrivateMemorySize64=6045696; PrivilegedProcessorTime=; ProcessName=AdAppMgrSvc; ProcessorAffinity=; Responding=True; SessionId=0; StartInfo=System.Diagnostics.ProcessStartInfo; StartTime=; SynchronizingObject=; Threads=System.Diagnostics.ProcessThreadCollection; UserProcessorTime=; VirtualMemorySize64=110989312; EnableRaisingEvents=False; StandardInput=; StandardOutput=; StandardError=; WorkingSet64=9519104; Site=; Container=} 
1

你是正确的:

  • |管道对象下成功/输出流

当你从一个小命令到另一个管道对象,你不想接收cmdlet来接收错误,警告,调试消息或详细消息以及它旨在处理的对象。

因此,管道运算符(|)实际上将对象沿着输出流(流#1)进行管理。

  • >将输出发送到指定的文件
  • >>输出追加到指定的文件
约重定向

的更多信息:https://msdn.microsoft.com/powershell/reference/5.1/Microsoft.PowerShell.Core/about/about_Redirection

相关问题