Jacek
Jacek

Reputation: 12053

Powershell: differences between | and >?

In PowerShell, what are differences between | and >?

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

Am I right to assume | moves the current result to the next block in the pipeline and > saves data to a file?

Are there any other differences?

Upvotes: 2

Views: 671

Answers (2)

Clijsters
Clijsters

Reputation: 4256

(not fully) yes.

| and > are two different things.

> is a so called redirection operator.

A redirection operator redirects the output of a stream to a file or another stream. The pipeline operator pipes the return object of a cmdlet or funtion to the next one (or the end of the pipeline). While the pipe pumps the whole object with its properties, the redirect pipes just its output. We can illustrate this with a simple example:

#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

Outputs

System.Diagnostics.Process (AdAppMgrSvc)

A try to convert the object to a string.


#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

Outputs

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

A third example will show the capabilities of the pipe operator:

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

This will output a Hashtable with all process's properties:

@{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=}

Upvotes: 5

Mark Wragg
Mark Wragg

Reputation: 23355

You are correct:

  • | pipes objects down the success/output stream

When you pipe objects from one cmdlet to another, you don't want the receiving cmdlet to receive errors, warnings, debugging messages, or verbose messages along with the objects that it's designed to process.

So, the pipe operator (|) actually pipes objects down the output stream (stream #1).

  • > Sends output to a specified file
  • >> Appends output to a specified file

More information about redirection: https://msdn.microsoft.com/powershell/reference/5.1/Microsoft.PowerShell.Core/about/about_Redirection

Upvotes: 1

Related Questions