Search notes:
Script: touch.ps1
touch.ps1 is a very basic
PowerShell script which mimicks the (
IMHO) two most important use cases of the Unix
touch command:
- It creates a file if it does not exist, or
- Updates an existing file's
lastAccessTime and lastWriteTime with the current timestamp (get-date).
The script accepts multiple files as string array ([string[]]), that is, their names must be separated by commas:
PS C:\Users\Rene> touch foo.txt, bar,txt, baz.txt
Source code
param (
[string[]] $fileList
)
set-strictMode -version 3
foreach ($fileName in $fileList) {
if (test-path $fileName) {
$file = get-item $fileName
$file.lastAccessTime = get-date
$file.lastWriteTime = $file.lastAccessTime
}
else {
$null = new-item $fileName
}
}