Powershell:|之间的差异 和>?

Jac*_*cek 2 powershell

在PowerShell中,|和之间有什么区别>

dir | CLIP #move data to clipboard
dir > CLIP #not moving, creating file CLIP (no extension)
Run Code Online (Sandbox Code Playgroud)

我是否正确假设|将当前结果移动到管道中的下一个块并将>数据保存到文件中?

还有其他差异吗?

Cli*_*ers 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
Run Code Online (Sandbox Code Playgroud)

输出

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
Run Code Online (Sandbox Code Playgroud)

输出

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

第三个例子将显示管道操作员的功能:

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

这将输出一个包含所有进程属性的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=}
Run Code Online (Sandbox Code Playgroud)