12

I am attempting to parse a file name in a folder and store parts of the filename in variables. Check! I then want to take one of the variables and check if that folder name exists in a different location, and if it does not create it. If I use Write-Host the folder name is a valid path, and the folder name does not exist, but upon execution of the script the folder still is not created.

What should I do to create the folder if it does not exist?

$fileDirectory = "C:\Test\"
$ParentDir = "C:\Completed\"
foreach ($file in Get-ChildItem $fileDirectory){

    $parts =$file.Name -split '\.'

    $ManagerName = $parts[0].Trim()
    $TwoDigitMonth = $parts[1].substring(0,3)
    $TwoDigitYear = $parts[1].substring(3,3)

    $FolderToCreate = Join-Path -Path $ParentDir -ChildPath $ManagerName

    If(!(Test-Path -path "$FolderToCreate\"))
    {
        #if it does not create it
        New-Item -ItemType -type Directory -Force -Path $FolderToCreate
    }

}
  • 1
    try this: New-Item -ItemType Directory -Force -Path C:\Path\That\May\Or\May\Not\Exist – Farzad J Apr 27 '17 at 13:47
  • If the folder exists, New-Item won't create it. So the question is really how to suppress the error, right? – Andreas Dec 13 '19 at 09:43
  • 1
    **See Also**: [Create directory if it does not exist](https://stackoverflow.com/q/16906170/1366033) – KyleMit Dec 27 '21 at 22:23

4 Answers4

20
if (!(Test-Path $FolderToCreate -PathType Container)) {
    New-Item -ItemType Directory -Force -Path $FolderToCreate
}
David Brabant
  • 41,623
  • 16
  • 83
  • 111
5

Try using the -Force flag - it checks for every single subdirectory when they don't exist, it would simply create it and go for the next one and never throw an error.

In the example below, you need 7 nested sub-directories, with one single line you can create anyone that doesn't exist.

You can also re-run it as many as times as you like, and it is designed to never throw an error!

New-Item -ItemType Directory -Force -Path C:\Path\That\May\Or\May\Not\Exist
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Farzad J
  • 1,089
  • 2
  • 14
  • 28
  • Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Apr 27 '17 at 14:50
  • Upvoting for pointing the need of Force for the creation of the whole tree. – GermanC Feb 21 '18 at 19:59
2

other solution :

if (![System.IO.Directory]::Exists($FolderToCreate ))
{
     New-Item -ItemType Directory -Force -Path $FolderToCreate
}
Esperento57
  • 16,521
  • 3
  • 39
  • 45
1

One-liner. Does nothing if it already exists.

[IO.Directory]::CreateDirectory($Path)
angularsen
  • 8,160
  • 1
  • 69
  • 83