header image

Masking output

Posted by: | August 8, 2015 Comments Off on Masking output |

A question on the forum on how to stop the output from New-Item raises an important point. Many cmdlets produce output when run for example

£> New-Item -Type File -Name test.txt

    Directory: C:\Test2

Mode                LastWriteTime         Length Name
—-                ————-         —— —-
-a—-       08/08/2015     14:14              0 test.txt

 

There are times when you want to hide that output. The converse situation where you want output from cmdlets that don’t normally produce it can be resolved with the –Passthru parameter where present or by using –verbose.

 

You have a number of choices when trying to mask output

£> New-Item -Type File -Name test1.txt | Out-Null
£> [void](New-Item -Type File -Name test2.txt)
£> New-Item -Type File -Name test3.txt > $null
£> $nf = New-Item -Type File -Name test4.txt

 

Any of the above techniques will work. I prefer the first one as I can develop and test the code showing output and then pipe to out-null to mask it

under: PowerShell Basics

Comments are closed.

Categories