在PowerShell中,|
和>
之間有什麼區別?Powershell:|之間的區別和>?
dir | CLIP #move data to clipboard
dir > CLIP #not moving, creating file CLIP (no extension)
我說得對假設|
當前結果移動到下一個塊的管道和>
將數據保存到一個文件?
還有其他區別嗎?
在PowerShell中,|
和>
之間有什麼區別?Powershell:|之間的區別和>?
dir | CLIP #move data to clipboard
dir > CLIP #not moving, creating file CLIP (no extension)
我說得對假設|
當前結果移動到下一個塊的管道和>
將數據保存到一個文件?
還有其他區別嗎?
(不完全)是的。
|
和>
是兩個不同的東西。
>
是一個所謂的重定向操作符。
重定向操作符將流的輸出重定向到文件或其他流。管道操作員將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=}
你是正確的:
|
管道對象下成功/輸出流當你從一個小命令到另一個管道對象,你不想接收cmdlet來接收錯誤,警告,調試消息或詳細消息以及它旨在處理的對象。
因此,管道運算符(|)實際上將對象沿着輸出流(流#1)進行管理。
>
將輸出發送到指定的文件>>
輸出追加到指定的文件
重定向操作符'>'也將輸出到一個字符串,而管'|'保持對象作爲是 –