As Lee Dailey points out, touch
is neither a PowerShell command nor a standard utility (external program) on Windows; by contrast, on Unix-like platforms touch
is a standard utility with dual purpose:
(a) When given existing file paths, the files' last-modified (.LastWriteTime
) and last-accessed (.LastAccessTime
) timestamps are set to the current point in time, by default.
(b) When given non-existing file paths, such files are created, by default.
There is no equivalent command in PowerShell (as of PowerShell 7.2), but you can use existing commands to implement (a) and (b) separately, and you can write a custom script or function that provides both (a) and (b) in a manner similar to the Unix touch
utility:
# Update the last-modified and last-accessed timestamps of existing file app.js
$file = Get-Item -LiteralPath app.js
$file.LastWriteTime = $file.LastAccessTime = Get-Date
- PowerShell implementation of (b), using the
New-Item
cmdlet:
# Create file app.js in the current dir.
# * If such a file already exists, an error will occur.
# * If you specify -Force, no error will occur, but the existing file will be
# *truncated* (reset to an empty file).
$file = New-Item app.js
Note: When a file is created, it is a 0
-byte file by default, but you may pass content via the -Value
parameter.
Jeroen Mostert suggests the following technique to implement the conditional nature
of the touch
utility's file creation:
# * If app.js exists, leaves it untouched (timestamps are *not* updated)
# * Otherwise, create it.
Add-Content app.js $null
Note:
To get touch
-like behavior, (a) (updating the last-write and last-accessed timestamps) must still be implemented separately.
At least hypothetically the Add-Content
approach can fail, namely if an existing target file is read-only - whereas manipulating such a file's timestamps may still work. (While you could suppress the error, doing so could make you miss true failures.)
The next section points to a custom function that wraps both (a) and (b), while also providing additional functionality that the Unix touch
utility offers.
A custom function named Touch-File
that implements most of the functionality of the Unix touch
utility - which has several options to provide additional behaviors beyond the defaults described above - is available from this MIT-licensed Gist.
Assuming you have looked at the linked code to ensure that it is safe (which I can personally assure you of, but you should always check), you can directly download and define it the current session as follows, which also provides guidance on how to make it available in future sessions:
irm https://gist.github.com/mklement0/82ed8e73bb1d17c5ff7b57d958db2872/raw/Touch-File.ps1 | iex
Note: Touch
is not an approved verb in PowerShell, but it was chosen nonetheless,
because none of the approved verbs can adequately convey the core functionality of
this command.
Sample call:
# * If app.js exists, update its last-write timestamp to now.
# * Otherwise, create it (as an empty file).
Touch-File app.js