Author Posts

July 2, 2015 at 12:55 am

I have a requirement.

I have to create a folder called test001. The issues is this:
– It is on the C Drive
– Folder is C:\folder001
– However inside folder001, there are subfolders:

Within each of these, there is the folder called "copyhere". This is where I want the folder, test001 to be created.

How can I do this? Is this possible with mkdir?

PLEASE NOTE: If it exits, then dont create it.

July 2, 2015 at 2:31 am

This should do the trick, worked for me when i tested.
I guess it could be done with mkdir but i like to use only powershell commandlets in my scripts if possible.

New-Item -ItemType Directory -Path C:\newfolder works as well as mkdir

Get-ChildItem .\folder001 -Recurse | Where-Object {$_.PsIsContainer} | % {
    If($_.Name -eq "copyhere"){
        If((Test-Path "$($_.FullName)\test001") -eq $false) {
            New-Item -ItemType Directory -Force -Path "$($_.FullName)\test001"

July 2, 2015 at 8:13 am

Could use wild cards with Get-ChildItem to find the folders you want to work with:

Get-ChildItem C:\folder001\???\copyhere | Foreach { New-Item -Path (Join-Path $_ test001) -ItemType Directory -Force }

I tested this after creating your directory structure this way:

$paths = 1..4 | Foreach { 'C:\folder001\{0:000}\copyhere' -f $_ }
New-Item -Path $paths -ItemType Directory -Force

July 2, 2015 at 10:47 pm

That's a much nicer solution there Nathan 🙂