Create a New Empty File with the PowerShell Out-File Cmdlet

A few weeks ago I was setting up OpenSSL on a Windows machine and I was following a Linux tutorial which used the “touch” command to create a new empty file.

To accomplish the task of creating a new empty file with PowerShell, the most straight forward way is to use the New-Item cmdlet:

New-Item -Name EmptyFile.txt -ItemType File

create-newemptyfile1a.jpg

I inadvertently discovered another way to create a new empty file with PowerShell which I thought I would share with you guys, the readers of my blog.

You may see some people piping $null to the Out-File cmdlet, but when you pipe $null it doesn’t produce any output so you could simply use the Out-File cmdlet by itself:

Out-File -FilePath EmptyFile2.txt

create-newemptyfile2a.jpg

Even though the length of the file created using Out-File is two instead of zero, it is indeed an empty file.

If I were typing the previous command interactively in the PowerShell console, I would omit the FilePath positional parameter, but I consider it to be a best practice to always use full cmdlet and parameter names in my blog articles.

ยต