Tuan Do
Tuan Do

Reputation: 11

Creating multiple folders and subfolder in Powershell

I need to create a folder and subfolder with name from E1 to E10 and inside each folder have 10 more folders, how do i do that in PowerShell ISE?

Upvotes: 1

Views: 1552

Answers (1)

mklement0
mklement0

Reputation: 437111

The following creates subfolders E1 through E10, and, inside each, subfolders F1 through F10:

# Creates subdirectories and returns info objects about them.
# -Force means that no error is reported for preexisting subdirs.
# Use $null = New-Item ... to suppress output.
New-Item -Type Directory -Force `
         -Path (1..10 -replace '^', 'E').ForEach({ 1..10 -replace '^', "$_\F" })

Note:

  • -replace '^', '...' is a simple way to prepend text to each input array element (each of the sequence numbers created with the .., the range operator, in this case).
  • -replace '$', '...' would append.
  • To insert the input in an arbitrary position, use ^.* to match the entire input and $& to refer to it in the replacemenent text; e.g.
    -replace '^.*', 'Before-$&-After'

Upvotes: 2

Related Questions