Powershell: How to Use Equivalent of “touch” Command


In a Linux environment, you can use the touch command to create an empty file with a specific name.

For example, you can use the following syntax to create an empty file named my_file.txt in the current directory:

touch my_file.txt

To replicate this behavior in PowerShell, we can use the New-Item cmdlet:

New-Item my_file.txt

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

By default, the New-Item cmdlet will display the path of the new file along with its creation date and time.

If you’d like to suppress any output when using the New-Item cmdlet, you can assign the results to $null as follows:

$null = New-Item my_file.txt

The following example shows how to use this syntax in practice.

Example: How to Use Equivalent of “touch” in PowerShell

Suppose that we would like create a new file named my_file.txt in our current directory.

Before we do so, we can first use the following syntax to view all of the files in our current directory:

Get-ChildItem -File

The following screenshot shows how to use this syntax in practice:

We can see that the current directory has three .txt files in it.

We can use the following syntax to create a new file named my_file.txt in our current directory:

New-Item my_file.txt

The following screenshot shows how to use this syntax in practice:

PowerShell touch equivalent

Notice that when we use the Get-ChildItem cmdlet again to view the updated contents of our directory we can see that a new file has been added to it named my_file.txt.

If you’d like to suppress any output when using the New-Item cmdlet to create this new file, you can use the following syntax:

$null = New-Item my_file.txt

The following screenshot shows how to use this syntax in practice:

Notice that a new item named my_file.txt has been created and no output was shown this time when we used the New-Item cmdlet to create the file.

Note: You can find the complete documentation for the New-Item cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Delete File if it Exists
PowerShell: How to Find Empty Folders
PowerShell: Check if File Has Been Modified in last 24 Hours

Leave a Reply