Multiple directories: Powershell equivalent of "mkdir dir{1..9}"?

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.


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