2

I need to create 3 folders named AAA,BBB,CCC at location :c\temp\test

I tried the below codes:

$local = "C:\Temp\Test"
$Users = $local+"\AAA\",$local+"\BBB\",$local+"\CCC\"
New-Item -Item Type directory $Users

It is giving me error New-Item : A positional parameter cannot be found that accepts argument

I also tried

$local = "C:\Temp\Test"
$Users = $local+"\AAA\",$local+"\BBB\",$local+"\CCC\"
md $Users

what am I missing here??

2
  • are the folders nested or do you want to have them in the same location $local? Commented May 19, 2017 at 21:37
  • I want to have them at same location Commented May 19, 2017 at 21:42

4 Answers 4

4

BenH's helpful answer offers an effective solution.

Why your command didn't work:

Your problem was one of operator precedence:

$local+"\AAA\",$local+"\BBB\",$local+ "\CCC\"

was interpreted as:

$local + ("\AAA\",$local) + ("\BBB\",$local) +"\CCC\"

resulting in a single string rather than the desired array, because the LHS - $local is a string.

Perhaps surprisingly, ,, the array-construction operator, has higher precedence than + - see Get-Help about_Operator_Precedence.


Therefore, using (...) for precedence would have made your command work:

$Users = ($local+"\AAA\"), ($local+"\BBB\"), ($local+"\CCC\")

Using expandable (interpolating) strings (inside "..."), as in BenH's answer, is more concise, but still:

  • requires defining auxiliary variable $local first

  • requires referencing $local in each expandable (interpolating) string ("...")


A DRY alternative is (% is an alias for ForEach-Object):

$Users = 'AAA', 'BBB', 'CCC' | % { "C:\Temp\$_\" }

Note that using a pipeline to achieve this is somewhat slow, but that won't matter in most cases.


Using the foreach statement is a little more verbose, but performs better:

$Users = foreach($name in 'AAA', 'BBB', 'CCC') { "C:\Temp\$name\" }
Sign up to request clarification or add additional context in comments.

Comments

1

For you definition of $users either put the variable inside the quotes or do the string concatenation in a subexpression e.g. $(). Also you had an extra space in the ItemType Parameter:

$local = "C:\Temp\Test"
$Users = "$local\AAA\","$local\BBB\","$local\CCC\"
New-Item -ItemType directory $Users

Comments

0

You can put all the directory path values in an array and create new directories at a single run.

@('D:/NewFolder','D:/NewFolder1/NewFolder_app','D:/NewFolder1/NewFolder_app_pool','C:/NewTestFolder101') | foreach {New-Item -Path $_ -ItemType Directory -Force}

It will create all the directories at once. For getting some directories as well as some files, declare two different arrays. The first one is for directories and the second one for the files. Rest of the script will be same except: Item-Type will be file instead of directory

Comments

0

why not just use Mkdir $(001..10 | %{“testfolders-$_”})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.