2

I know that this is a little dumb, but I've setup a custom profile that I end with a Clear-Host commandlet for Powershell and I hate that output from the previous commandlets flashes on the screen. I decided to send the output to null in an attempt to suppress the output, e.g.:

New-Alias -Name note -Value notpad.exe > $null

It seems to be working, but I'm not entirely certain as to what the command is doing. Is it okay to do this?

briantist
  • 45,546
  • 6
  • 82
  • 127
tayopi
  • 295
  • 5
  • 15

2 Answers2

4

> is a redirection operator. This is common in the computing world, in unix (> /dev/null), and in Windows (> NUL). Often it's used to write to files or devices. In this case, you're sending it nowhere.

Other ways to do this in PowerShell:

Get-Something | Out-Null
[void](Get-Something)
$null = Get-Something

Also here's a performance comparison between the methods.

Community
  • 1
  • 1
briantist
  • 45,546
  • 6
  • 82
  • 127
  • 1
    The small sample set used in the answer you linked, obfuscates the fact that `$null` assignment and `void` casting is faster than redirection. Try [this one](https://gist.githubusercontent.com/IISResetMe/610deb873a96f331039d/raw/1175e21dcc410e4acbd41789c87ea7cf1e7ee666/Test-NullPerformance.ps1) with 1 million iterations or more – Mathias R. Jessen May 05 '16 at 18:45
2

Adding > $null to the end of a pipeline (or a single expression) throws away any result that would go to the default output.

It is one (of several) way to remove excess output (or return data if in a function).

In the case of New-Alias it would normally return a result representing the new alias, but sometimes that output gets in the way.

Richard
  • 106,783
  • 21
  • 203
  • 265