Touch

Change the date/time of a file.

Changing the last modified date/time of a file (or folder) in PowerShell can be a simple one liner, to set the date/time to 1976 Dec 31st:

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

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 messed up due to 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:

StampMe - Rename a file with the current Date/Time


© Copyright SS64.com 1999-2013
Some rights reserved