Home Ask Login Register

Developers Planet

Your answer is one click away!

Roger February 2016

Create Directory with ls and Create-Item

I wanted to create in another directory the same folders of another directory (which contains about 234 folders).

My ideas was

Ls -Name | New-Item -ItemType Directory -Path D:\Test\

I get then multiple times the error that the D:\Test path already exsits.


vonPryz February 2016

This doesn't work, as the directory creation part New-Item -ItemType never uses the value from pipeline.

What's more, you don't filter the output from ls, so any non-directory files are processed too.

# Get a list of folders only
gci | ? { $_.psIsContainer } | % { 
  # create a dir. Uses join-path and $_.name to combine new root dir with directory's name
  new-item -itemtype directory -path $(join-path "d:\myroot" $_.name)

Post Status

Asked in February 2016
Viewed 1,582 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life