11

What is the syntax to create multiple directories with PowerShells md (or mkdir, New-Item...) equivalent to the 'nix command mkdir ch{1..9} i.e.

~/parent_dir/  
ch1/  
ch2/  
ch3/  
ch4/  
ch5/  
ch6/  
ch7/  
ch8/  
ch9/  

I've looked in the man pages and get-help for examples, but I do not know the syntax for PowerShell to do such a simple thing. Thank you.

Uwe Keim
  • 2,112
MmmHmm
  • 768

4 Answers4

23

You don't need to invoke mkdir multiple times, because New-Item can take an array of paths. For example:

mkdir $(1..9 | %{"ch$_"})

@DavidPostill has explained most of the concepts in his answer. This also takes advantage of string interpolation instead of performing an explicit concatenation. Additionally, the % shorthand is used instead of ForEach-Object, but has the same meaning.

Unfortunately, there does not appear to be an easy way to interpolate a string into an array of strings as in bash.

Bob
  • 63,170
17

What is the syntax to create multiple directories with PowerShell

Use the following command:

0..9 | foreach $_{ New-Item -ItemType directory -Name $("ch" + $_) }

How it works:

  • 0..9 the range operator .. generates the sequence of numbers 0, 1, ... 9
  • the numbers are pipelined | to the next command
  • foreach loops (through each number in turn)
  • { ... } is a script block
  • New-Item -ItemType directory -Name $("ch" + $_) creates the directories
  • $_ is an automatic variable that represents the current object in the pipeline (the number)

Example:

> 0..9 | foreach $_{ New-Item -ItemType directory -Name $("ch" + $_) }


    Directory: F:\test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       25/09/2016     14:57                ch0
d-----       25/09/2016     14:57                ch1
d-----       25/09/2016     14:57                ch2
d-----       25/09/2016     14:57                ch3
d-----       25/09/2016     14:57                ch4
d-----       25/09/2016     14:57                ch5
d-----       25/09/2016     14:57                ch6
d-----       25/09/2016     14:57                ch7
d-----       25/09/2016     14:57                ch8
d-----       25/09/2016     14:57                ch9
DavidPostill
  • 162,382
2

Create multiple directories underneath the current directory:

mkdir ('abc','def','jkl') 

The above is a short-hand version of the following. Notice the at-sign included in front of the array of strings and the use of named parameters:

mkdir -Path @('abc','def','jkl')

And if you want to go all the way, the full native command would be:

New-Item -Path @('abc','def','jkl') -ItemType Directory

When using the PowerShell command-line, I use the short version.

When writing a script, especially one for others (who might be new to PowerShell), I tend to write the full native command.

Choose whatever works best for you.

1

I would use for loop version as it is easy to remember and can be applied to many situations. Even it can be used for multiple command.

For an equivalent of this bash command:

for i in {1..9}; do
mkdir ch$i
done

...in PowerShell use:

for($i=1;
$i -le 10;
$i++)
{md ch$i}
MmmHmm
  • 768