用 New-Item 创建文件和文件夹

PowerShell 里被遗忘的“touch/mkdir”命令,还能创建硬链接和 junction。

Windows文件创建PowerShell低风险
运行 New-Item -Path <路径> -ItemType File(或 -ItemType Directory)即可创建文件或文件夹。

01命令

命令
$ New-Item -Path 'C:\Temp\notes.txt' -ItemType File ; New-Item -Path 'C:\Temp\logs' -ItemType Directory

02作用说明

New-Item 是 PowerShell 的通用创建命令:-ItemType 支持 File、Directory,Windows 下还支持 SymbolicLink、Junction 和 HardLink。父目录不存在会自动一并创建,一条命令就能建出深层路径。-Value 可以在创建时写入内容,-Force 则替换已存在的文件。

03风险

风险低风险
使用条件PowerShell
!注意: 对已存在的文件执行 New-Item -ItemType File 会报错;加 -Force 后虽然能执行,但会把文件清空,内容丢失。

04撤销

撤销
$ Remove-Item 'C:\Temp\notes.txt'

05来源

  • New-Item - PowerShell
    SS64 · technical · 2026-09-17
    依据: SS64 lists supported -ItemType values including File, Directory, SymbolicLink, Junction and HardLink.
  • New-Item | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    依据: Microsoft documents New-Item with -ItemType File/Directory/SymbolicLink/Junction/HardLink and -Force to replace an existing file.

最后验证: 2026-09-17 · 分类: 文件与文件夹

06相关方案