Powershell script to backup folders older than 7 days

I have a folder X:/EmpInfo that contains many different folders. Each of these folders contains about 5-10 files.

I need to backup folders that have a modified date newer than 7 days ago. That is, modified would mean new files were added to the folder or an existing file was modified.

Example: If I run it today (11/08), it'll backup all folders in X:/EmpInfo with a modification date of 11/01 to current time today (when the script is run). It should move the entire folder, not just the modified files. Overwrite any existing folders.

3 Answers

  1. Leonard- Reply


    It might not be "something for real like Perl" but PowerShell can handle that pretty easily. This should get you started:

    $newFolders = dir X:\EmpInfo | ? {$_.PSIsContainer} | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-7)} 
    $newFolders | % { copy $_.FullName  c:\temp\archive -Recurse -Force}

    Have fun with the pipeline!

  2. Leopold- Reply


    My answer is a combination of the answers of MrKWatkins and Eric Nicholson. In your question you clarify that what you actually want is to copy directories where new files were added or existing files were copied. The behaviour of the last modification date for the containing directory will be different on different filesystems:

    • NTFS: On an NTFS file system, the modified date of a folder DOES change if the contents of the folder change.
    • FAT: On a FAT file system, the modified date of a folder does not change if the contents of the folder change.

    Description of NTFS date and time stamps for files and folders

    Therefore ideally we should first test the filesystem type of the source directory before deciding on how to determine what directories to copy:

    function Copy-ModifiedSubdirectory {
      param ($sourcefolder, $destinationfolder, [DateTime] $modifieddate)
      # Escaping source folder for use in wmi query
      $escapedsourcefolder = $sourcefolder -replace "\\","\\"
      # Determine what filesystem our folder is on
      $FSName = (get-wmiobject -query "select FSName from CIM_Directory where name = '$escapedsourcefolder'").FSName
      if ($FSName -eq "NTFS") # The Eric Nicholson way
        $FoldersToCopy = get-childitem $sourcefolder | where {$_.PSIsContainer} | where {$_.LastWriteTime -ge $modifieddate} 
      elseif ($FSName -eq "FAT32") # The MrKWatkins way
        $FoldersToCopy = get-childitem $sourcefolder | where {$_.PSIsContainer} | ? { Get-ChildItem $($_.fullname) -Recurse | ? { $_.LastWriteTime -ge $modifieddate } }
        Write-Error "Unable to Copy: File System of $sourcefolder is unknown"
      # Actual copy
      $FoldersToCopy | % { copy $_.FullName $destinationfolder -Recurse -Force}

    To use the function:

    $sevendaysago = ((get-date).adddays(-7))
    copy-modifiedsubdirectory  X:\EmpInfo Y:\Archive $sevendaysago
  3. Loren- Reply


    Here is a rough and ready script to get you started. I'm sure there are better ways to a lot of this...

    $sevenDaysAgo = (Get-Date).AddDays(-7);
    # Get the directories in X:\EmpInfo.
    $directories = Get-ChildItem . | Where-Object { $_.PSIsContainer };
    # Loop through the directories.
    foreach($directory in $directories)
        # Check in the directory for a file within the last seven days.
        if (Get-ChildItem .\UnitTests -Recurse | Where-Object { $_.LastWriteTime -ge $sevenDaysAgo })

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>