iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐳

How to Create Files and Parent Directories in PowerShell

に公開

TL;DR

Use the -Force option.

PowerShell
ni -Force .github/workflows/deploy.yml

Purpose

When creating a file in PowerShell, I want to create the folders and the file all at once.
However, creating it normally with ni results in an error.

PowerShell
ni .github/workflows/deploy.yml
# An error occurs if the intermediate directories (folders) do not exist!

So, try adding -Force.

PowerShell
New-Item -Force .github/workflows/deploy.yml
# Or
ni -Force .github/workflows/deploy.yml

By doing this, it will automatically create the intermediate folders if they don't exist.

dir
Current folder/
└── .github/          <-- Automatically created
    └── workflows/    <-- Automatically created
        └── deploy.yml <-- File created

That's it.

Discussion