The touch
command in Linux is used to change a file’s “Access“, “Modify” and “Change” timestamps to the current time and date, but if the file doesn’t exist, the touch
command creates it.
If you simply want to create an empty file from the command-line prompt (CMD) or a Windows PowerShell – the type
and copy
commands can be considered as a Windows touch
command equivalent.
The file timestamps in Windows can be changed using the built-in PowerShell commands.
Cool Tip: Windows cat
command equivalent in CMD and PowerShell! Read more →
Windows `Touch` Command Equivalent
To create a new file, as a Windows touch
equivalent, you can use one of these commands:
C:\> type nul >> "file.txt"
- or -
C:\> copy nul "file.txt"
In the PowerShell the new file can be also create as follows:
PS C:\> New-Item "file.txt"
- or -
PS C:\> ni "file.txt"
To change a file timestamps to the current time and date, execute the following commands from the PowerShell:
PS C:\> (Get-Item "file.txt").CreationTime=$(Get-Date -format o) PS C:\> (Get-Item "file.txt").LastWriteTime=$(Get-Date -format o) PS C:\> (Get-Item "file.txt").LastAccessTime=$(Get-Date -format o)
Cool Tip: Windows grep
command equivalent in CMD and PowerShell! Read more →
To set the specific timestamps, execute:
PS C:\> (Get-Item "file.txt").CreationTime=("01 March 2020 09:00:00") PS C:\> (Get-Item "file.txt").LastWriteTime=("20 April 2020 17:00:00") PS C:\> (Get-Item "file.txt").LastAccessTime=("20 April 2020 17:00:00")
The timestamps can be displayed using the following command:
PS C:\> Get-Item file.txt | Format-List CreationTime, LastAccessTime, LastWriteTime
This is extremely helpful information – thanks for the post! My particular use-case is for a VeraCrypt (.vc) archive. By design, VeraCrypt does not automatically update the file write time when a “hidden” container inside a container file is modified. This is done for security purposes, but it prevents my RoboCopy and some differential backup scripts from duplicating the file. It also can cause issues with OneDrive and other drive sync software. Your PowerShell command updating the LastWriteTime avoids this issue, and it’s something I can add to my manual backup scripts.
An alternative is that id windows subsystem for linuzx ( WSL ) is installed , you can launch a bash shell and run touch .
Very nice, thanks for the helpful information!
Another alternative to create a new file in powershell is New-Item. So `New-Item file.txt`. New-Item has an alias of ni so `ni file.txt` will also work.
This is what I was looking for, thanks!
Thanks. Have added to the article.