How-to: Change the date/time of a file (Touch)

Changing the last modified date/time of a file (or folder) in PowerShell can be a simple one liner:

for example, set the last modified date/time to 1976 Dec 31st:

PS C:> (dir sample_file.txt).LastWriteTime = New-object DateTime 1976,12,31

Set the last modified date/time to 30th June 2020 at 23:45:

PS C:> (dir sample_file.txt).LastWriteTime = New-object DateTime 2020,06,30, 23,45,00

The earliest/latest dates that can be set by Win32/PowerShell:
1600,01,01, 00,00,00
9999,01,01, 00,00,00

The earliest/latest dates that can be read by Windows Explorer:
1980,01,01, 00,00,00
2107,12,31, 23,59,00

Set the last modified date/time to Today:

PS C:> (dir sample_file.txt).LastWriteTime = Get-Date

Set the last modified date/time to Today for all files and subdirectories/ files:

PS C:> Get-ChildItem C:\demo\ * -recurse | ForEach-Object{$_.LastWriteTime = Get-Date}

The function below implements a fully featured PowerShell version of the Unix touch command. It accepts piped input and if the file does not already exist it will be created. There are options to change only the Modification time or Last access time (-only_modification or -only_access)

Function Set-FileTime{
  param(
    [string[]]$paths,
    [bool]$only_modification = $false,
    [bool]$only_access = $false
  )

  begin {
    function updateFileSystemInfo([System.IO.FileSystemInfo]$fsInfo) {
      $datetime = get-date
      if ( $only_access )
      {
         $fsInfo.LastAccessTime = $datetime
      }
      elseif ( $only_modification )
      {
         $fsInfo.LastWriteTime = $datetime
      }
      else
      {
         $fsInfo.CreationTime = $datetime
         $fsInfo.LastWriteTime = $datetime
         $fsInfo.LastAccessTime = $datetime
       }
    }
   
    function touchExistingFile($arg) {
      if ($arg -is [System.IO.FileSystemInfo]) {
        updateFileSystemInfo($arg)
      }
      else {
        $resolvedPaths = resolve-path $arg
        foreach ($rpath in $resolvedPaths) {
          if (test-path -type Container $rpath) {
            $fsInfo = new-object System.IO.DirectoryInfo($rpath)
          }
          else {
            $fsInfo = new-object System.IO.FileInfo($rpath)
          }
          updateFileSystemInfo($fsInfo)
        }
      }
    }
   
    function touchNewFile([string]$path) {
      #$null > $path
      Set-Content -Path $path -value $null;
    }
  }
 
  process {
    if ($_) {
      if (test-path $_) {
        touchExistingFile($_)
      }
      else {
        touchNewFile($_)
      }
    }
  }
 
  end {
    if ($paths) {
      foreach ($path in $paths) {
        if (test-path $path) {
          touchExistingFile($path)
        }
        else {
          touchNewFile($path)
        }
      }
    }
  }
}

New-Alias touch Set-FileTime

This script is based on Keith Hill’s Touch-File script combined with Joe Pruitt’s touch script, just like the Unix Touch command this function can be used to create new empty files. These are completely empty: 0 bytes (not 2 bytes due to a CR/LF being added)

Note: PowerShell does not save functions or aliases permanently by default. So if you close and reopen PowerShell, this function will no longer be available. To make it permanent, add the function to your PowerShell $Profile file.

The PowerShell community extensions include a similar function, also called Set-FileTime (with the alias Touch) but that function will not create new files, (it throws an error if the file does not already exist).

Examples

Change the Creation + Modification + Last Access Date/time and if the file does not already exist, create it:

PS C:\> touch foo.txt

Change only the modification time:

PS C:\> touch foo.txt -only_modification

Change only the last access time:

PS C:\> touch foo.txt -only_access

Change multiple files:

PS C:\> touch *.bak
PS C:\> dir . -recurse -filter "*.xls" | touch

Files showing a data/time earlier than 1980 are likely to be corrupt - often permissions with incorrectly ordered ACLs.
List all files that are over 40 years old:

PS C:\> dir "\\server64\fileshare\" -recurse | where{$_.LastWriteTime -lt ((get-date).adddays(-14609))} > problems.txt

“Any fool can make things bigger, more complex, and more violent.
It takes a touch of genius, and a lot of courage, to move in the opposite direction” ~ Albert Einstein

Related PowerShell Cmdlets

Set-LastWrite - Reset Folder 'Last Modified' to the most recent file in the folder (PowerShell function).
StampMe - Rename a file with the current Date/Time.
set-eol - Change the line endings of a text file.


 
Copyright © 1999-2024 SS64.com
Some rights reserved