17

In cmd can be used command mkdir /data/rs1 /data/rs2 /data/rs3 like: cmd example

Everything is correct:

folders example

But how to realize it in powershell?

I'm trying to use quotes, for example: powershell

With quotes I'm getting just one folder and having the issue in powershell: folders example

mkdir : Could not find part of the path "rs3".
строка:1 знак:1
+ mkdir "/data/rs1 /data/rs2 /data/rs3"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\data\rs1 \data\rs2 \data\rs3:String) [New-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand

Without qoutes also the issue and haven't any folders folders example: powershell

mkdir : Can not find a positional parameter that takes an argument"/data/rs2".
строка:1 знак:1
+ mkdir /data/rs1 /data/rs2 /data/rs3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [mkdir], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,mkdir

To avoid the issues, I can write without spaces:

PS C:\data> mkdir /data/rs1/data/rs2/data/rs3

But it will be one folder rs1 that contains inside rs2 and rs3:

C:\data\rs1\data\rs2\data\rs3

I appreciate any help.

CC BY-SA 4.0

6 Answers 6

14

mkdir can create multiple directories in one go, so no need for foreach. you just have to spearate them by commas:

Here I created 3 folders (Hello, Hello2, Hello3) in a directory

PS C:\install> mkdir Hello,Hello2,Hello3


    Verzeichnis: C:\install


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       09.07.2018     10:39                Hello
d-----       09.07.2018     10:39                Hello2
d-----       09.07.2018     10:39                Hello3

Here I created 3 folders on separate subfolders in a directory:

PS C:\install> mkdir .\xy3\Hello, .\yz3\Hello2, .\tr3\Hello3


    Verzeichnis: C:\install\xy3


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       09.07.2018     10:42                Hello


    Verzeichnis: C:\install\yz3


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       09.07.2018     10:42                Hello2


    Verzeichnis: C:\install\tr3


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       09.07.2018     10:42                Hello3
CC BY-SA 4.0
2
11

There are a lot of ways doing this in powershell

1..3 | ForEach {MD ".\data\rs$_"}

or

'RS1','RS2','RS3' | % {New-Item -Name ".\data\$_" -ItemType 'Directory'}

or

for ($i=1;$i -le 3;$i++){MD ".\data\rs$i"}

or

MD .\data
Pushd .\data
$Folder = @('RS1','RS2','RS3')
Md $Folder

Where md is an alias for New-Item and
%,ForEach are aliases for ForEach-Object

CC BY-SA 4.0
2
  • Thanks, solved by using first variant. In my case it was PS C:\data> 1..3 | ForEach {MD "rs$_"}
    – invzbl3
    Jul 8, 2018 at 14:12
  • pushd is an alias for push-location and puts the current item to the top. Probably you do not need it but I am not sure. Maybe in the last solution a simple $ar='f1','f2',.. would also work.
    – Timo
    Oct 11, 2020 at 12:46
2

You could use foreach in PowerShell to get this done

ForEach ($Dir in ("Dir1", "Dir2", "Dir3", "Dir4"))
    {
        New-Item -ItemType Directory -Path [PATH]\$Dir
    } 

Read more about ForEach in PowerShell

CC BY-SA 4.0
1
2

i'm a lazy guy, so i tried to find a way to make this easily scalable. you can add a Write-Host to show which directories have been created or export-excel or even Transcript to a log. this is just a simple script.

#new-foldersMultiple.ps1

#Collect usernames for Directory names
$newUsers = Get-Content -Path "\\DFS\newUsers.csv"

#Set the HomeDrive path
$homeDrivePath = "D:\home\Users\"

#loop through each username to create their HomeDrive Directory
foreach ($newUser in $newUsers)
{

New-Item -ItemType directory -Name $newUser -Path $homeDrivePath
start-sleep 2

}
CC BY-SA 4.0
1

First of all, please don't use screenshot for things you can easily copy-paste as text.

The command in your screenshot is this:

mkdir \data\rs1 \data\rs2 \data\rs3

And this will work just fine in a script too, provided you are on the correct drive, in this example C:.

The command mkdir /data/rs1 /data/rs2 /data/rs3 is not the same thing, because as you can see, as the path parameters use / as path separator, which will not work in Windows.

Putting double-quotes around the list of paths doesn't help either. That way the double-quoted expression is treated as one single path, rather than 3 paths.

In short, just as mkdir \data\rs1 \data\rs2 \data\rs3 works in your command example, it works exactly the same way in a batch script.

CC BY-SA 4.0
0
0

The following command in powershell will create multiple directories (20) named "dir" in the directory that the command is executed.

mkdir $(1..20 | %{"dir$_"})

I think this command is the closest to the bash equivalent in Unix and Linux:

mkdir dir{1..20}
CC BY-SA 4.0
1
  • Welcome to Super User! Before answering an old question having an accepted answer (look for green ✓) as well as other answers ensure your answer adds something new or is otherwise helpful in relation to them. Here is a guide on How to Answer. There is also tour for the site tour, and help center for the help center. May 14, 2022 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.