How to Create an Empty File in PowerShell?

Do you need to create an empty file in PowerShell? This is a very common requirement for PowerShell administrators. In this tutorial, we’ll explore different methods to create an empty file using PowerShell.

To create an empty file in PowerShell, you can use the New-Item cmdlet with the syntax New-Item -Path .\example.txt -ItemType File, which is the most direct method. Alternatively, you can use the Out-File cmdlet by executing Out-File -FilePath .\example.txt, or use redirection with > .\example.txt to achieve the same result. These methods provide simple and quick ways to create an empty file using PowerShell scripting.

Create an Empty File in PowerShell

Let us discuss a few methods to create an empty file in PowerShell with examples and a complete script.

Method 1: Using the New-Item Cmdlet

The New-Item cmdlet is a straightforward way to create an empty file in PowerShell. This PowerShell cmdlet can create not just files but also directories, registry keys, and other item types.

Here’s an example of how to create an empty text file named example.txt in the current directory:

New-Item -Path C:\MyFolder\example.txt -ItemType File

This cmdlet creates a new file at the specified path. If you do not specify a path, the file will be created in the current directory.

You can see in the screenshot below that after I executed the PowerShell script using VS code, it created an empty file in the specified folder or directory.

Create an Empty File in PowerShell

Method 2: Using the Out-File Cmdlet

The Out-File cmdlet is typically used to send output to a file, but it can also be used to create an empty file by simply not providing any input.

For instance, to create an empty file named example.txt, you could use the following command:

Out-File -FilePath C:\MyFolder\example.txt

This command creates a new, blank text file at the specified location.

Method 3: Using Redirection Operators

In PowerShell, you can use redirection operators, similar to other shells like Bash. The > operator can be used to redirect output to a file, and if no output is specified, it results in an empty file.

Here’s how you can create an empty file with redirection in PowerShell:

> C:\MyFolder\example.txt

This will create an empty file named example.txt in the current directory.

Method 4: Using the fsutil Command

While not a PowerShell cmdlet, fsutil is a command-line utility that can be used within PowerShell to create an empty file. This is a utility native to Windows, so it may not be available in all environments.

To create an empty file using fsutil, you can run:

fsutil file createnew C:\MyFolder\example.txt 0

This command creates a new file named example.txt with a size of 0 bytes, effectively creating an empty file.

Method 5: Using the .NET Framework

PowerShell can access the .NET Framework, which provides another method to create an empty file. Here’s how you can do it:

[IO.File]::WriteAllText('.\example.txt', '')

This line of code uses the WriteAllText method of the File class from the System.IO namespace to create an empty file named example.txt.

Method 6: Using the New-TemporaryFile Cmdlet

If you need to create a temporary empty file, PowerShell provides the New-TemporaryFile cmdlet. This cmdlet creates an empty file with a .tmp extension in the user’s temp directory.

$tempFile = New-TemporaryFile

This command creates a temporary file and stores its file path in the $tempFile variable.

Method 7: Using echo with Redirection

Another simple method to create an empty file in PowerShell is to use the echo command with redirection. By echoing nothing into a file, you create an empty file.

echo $null > .\example.txt

This command creates an empty file named example.txt in the current directory.

Conclusion

Creating an empty file in PowerShell is easy and there are different ways to do it like by using built-in cmdlets like New-Item or Out-File, leveraging the .NET Framework, or employing traditional command-line utilities like fsutil, etc.

In this complete tutorial, I have explained how to create an empty file in PowerShell using different ways.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.