Tyler Jones
Tyler Jones

Reputation: 1343

What is the ">" operator in PowerShell?

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?

Upvotes: 2

Views: 454

Answers (2)

nochkin
nochkin

Reputation: 710

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

Upvotes: 5

Iain Samuel McLean Elder
Iain Samuel McLean Elder

Reputation: 20934

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.

Upvotes: 1

Related Questions