What is the `mkdir -p` equivalent in Powershell?

Viewed 383

In other words, in Powershell, how to create directories recursively, and not fail if each directory level already exist ?

1 Answers

The mkdir -p command does two things:

  • It creates directories (and files) recursively
  • It doesn't print an error if any depth of the path already exists.

To achieve this behavior for in Powershell, use:

  • New-Item which already creates paths recursively
  • -Force to fail silently if paths already exist

Hence, to create C:/x/y where x and y must be directories, use:

New-Item -ItemType Directory -Path C:/x/y -Force

To create C:/x/y where x is a directory and y a file, use:

New-Item -ItemType File -Path C:/x/y -Force

Related