PowerShell 空のファイルを作成する

PowerShell 空のファイルを作成する

PowerShellで、空のファイルを作成する方法を掲載してます。

環境

  • OS windows 11 home
  • powershell 5.1.22000.653

空のファイルを作成

空のファイルを作成するには、
「New-Item」「-type file」を指定することで
可能です。
※ここでは「c:\test\」に「foo.txt」という空のファイルを作成してます。

> New-Item c:\test\foo.txt -type file

実行画像

「foo.txt」が作成されていることが確認できます。

既にファイルが存在する場合

作成するファイルが既に存在する場合は、エラーとなります。

> New-Item c:\test\foo.txt -type file

New-Item : ファイル 'C:\test\foo.txt' は既に存在します。
発生場所 行:1 文字:1
+ New-Item c:\test\foo.txt -type file
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\test\foo.txt:String) [New-Item], IOException
    + FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand

上書きする場合は「-Force」を指定します。

> New-Item c:\test\foo.txt -type file -Force