17

Is there a way to use mkdir (aka md) in powershell without verbose output? Currently, the output is as follows:

PS C:\Users\myusername> mkdir foobar


    Directory: C:\Users\myusername


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        2016-12-07   9:35 AM            foobar
PS C:\Users\myusername>

Unless there's an error to report, I'd like it to be silent, as in

PS C:\Users\myusername> mkdir foobar
PS C:\Users\myusername>

Is there a way to do this? I'm using Powershell version 2.

  • 4
    How about `| Out-Null`? – user364455 Dec 07 '16 at 13:44
  • 1
    In which context would you want it to be silent? Just in a specific location in a script or always? – Seth Dec 07 '16 at 13:59
  • PetSerAl - That works. It preserves errors, too (at least, it doesn't redirect stderr to null) –  Dec 07 '16 at 14:19
  • Seth - Just in interactive use. It's not a serious problem, but I've found that the verbose output makes reading previous commands more difficult, especially in that it forces me to scroll my command prompt window up more than I would otherwise need to. –  Dec 07 '16 at 14:21
  • 6
    `mkdir | out-null`, `mkdir > $null`, `$null = mkdir`, `[void]mkdir` are your options, I always use `> $null` because it's faster than `| out-null` - see this for reference http://stackoverflow.com/questions/5260125/whats-the-better-cleaner-way-to-ignore-output-in-powershell – SimonS Dec 08 '16 at 08:45

3 Answers3

15

PetSerAl is correct, added to by SimonS
Out-Null is your best bet but as SimonS stated > $null is quicker

Lachie White
  • 324
  • 3
  • 10
3

Just to add another solution: mkdir returns an object and if I just execute the code below, I don't have any output. Further more, I can use $dir to make my own output if needed

$dir = mkdir c:\foo\bar

As a side note, I've tested this PowerShell Version

PS> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      15063  1155
0

Make a function

function silentMkdir { $sink = mkdir $args }

Make an alias in the current shell only that uses that function

Set-Alias -Name mkdir -Value silentMkdir -Scope Private

PS: I'm new to Powershell but this seemed to work. Without the -Scope Private mkdir will be changed for scripts called from this shell. With -Scope Private that issue seems to go away.

gman
  • 4,894
  • 4
  • 27
  • 31