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.

7 Answers 7

15

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
2
12

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

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

1
  • While the parentheses enclosing the collection entries ease reading, they are not required. I do like ss64.com but the Get-Help about_foreach or docs.microsoft.com are more detailed.
    – LotPings
    Jul 8, 2018 at 14:35
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

}
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.

0
1

I created the following to create a PowerShell script that will create folders and different subfolders underneath those folders based on a CSV file.

#Set Location for folder creation

Set-Location "S:\Folder" 

#Import the CSV file
$csvFile = Import-Csv "S:\Folder\FolderNames1.csv"

#Loop through each row in the CSV file
foreach ($row in $csvFile) {

    # Get the folder path
    $Folder = $row.Folder

    # Create the folder
    New-Item -ItemType Directory -Path $Folder

    # If the row contains subfolder names, create each subfolder
    if ($row.Subfolder) {
        foreach ($Subfolder in $row.Subfolder) {
            $subFolderPath = $folder + "\" + $Subfolder
            New-Item -ItemType Directory -Path $SubfolderPath
        }
    }
}

The CSV had the following format

Folder,Subfolder
Folder 1,Subfolder1
Folder 1,Subfolder2
Folder 1,Subfolder3
Folder 2,Subfolder4
Folder 2,Subfolder5
Folder 2,Subfolder6
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}
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .