9

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.

8

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

| improve this answer | |
  • Thanks, solved by using first variant. In my case it was PS C:\data> 1..3 | ForEach {MD "rs$_"} – invzbl3 Jul 8 '18 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 at 12:46
6

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
| improve this answer | |
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

| improve this answer | |
  • 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 '18 at 14:35
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.

| improve this answer | |

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.