19

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.

5
  • 4
    How about | Out-Null?
    – user364455
    Dec 7, 2016 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 7, 2016 at 13:59
  • PetSerAl - That works. It preserves errors, too (at least, it doesn't redirect stderr to null)
    – user672409
    Dec 7, 2016 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.
    – user672409
    Dec 7, 2016 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 stackoverflow.com/questions/5260125/…
    – SimonS
    Dec 8, 2016 at 8:45

3 Answers 3

17

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

4
  • Thanks! I would have marked PetSerAl's answer as correct, but since I can't this one gets the kudos.
    – user672409
    Dec 19, 2016 at 13:04
  • nope, doesn't work in PS for win 10.
    – FizxMike
    Apr 19, 2018 at 20:50
  • Instead, this seems to fail silently: [system.io.directory]::CreateDirectory("C:\test")
    – FizxMike
    Apr 19, 2018 at 20:53
  • 1
    @FizxMike sorry just seen this, works for me on a pretty regular basis on windows 10. Jul 2, 2018 at 7:27
4

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
1
  • This one seems to me as cool, clean and practical solution. Thanks.
    – Honza P.
    Mar 4, 2020 at 12:47
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.

You must log in to answer this question.