2

I've tried searching around for this, and I can't find an answer, partially because it's difficult to search for the ">" character and also because the prompt in PowerShell uses that character.

I've got an example that works well, but I don't know what this line is doing exactly:

New-Item $tempCmd -Force -Type file > $null

I get the New-Item call and its parameters, but what is "> $null" doing exactly? And specifically what role does ">" play in this statement?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tyler Jones
  • 1,283
  • 4
  • 18
  • 38
  • To pipe output to null http://stackoverflow.com/questions/5260125/whats-the-better-cleaner-way-to-ignore-output-in-powershell – Palmer Jun 20 '14 at 18:49

2 Answers2

5

The > character does output redirection. In an example it seems like it suppresses the output by redirecting it to null.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nochkin
  • 692
  • 6
  • 17
1

Microsoft has published a language specification for PowerShell 2.0 and PowerShell 3.0.

From version 3.0:

The redirection operator > takes the standard output from the pipeline and redirects it to the location designated by redirected-file-name, overwriting that location's current contents.

Your example has a null filename, so the output goes nowhere.

As nochkin says, people normally do this to stop a command from producing output. By default New-Item will output metadata about the new item to the host.

To acheive the same thing in a more readable way you can pipe the output to Out-Null.

New-Item $tempCmd -Force -Type file | Out-Null

From the documentation:

Deletes output instead of sending it down the pipeline.

Community
  • 1
  • 1
Iain Samuel McLean Elder
  • 19,791
  • 12
  • 64
  • 80