Occasionally, you may want to move files to an archive folder when they are older than a given number of days.
Here is an example that illustrates the basic strategy on how to identify outdated files, and how to move them to an archive:
#requires -Version 1 # how old (in days) would obsolete files be $Days = 14 # where to look for obsolete files $Path = $env:windir $Filter = '*.log' # where to move obsolete files $DestinationPath = 'c:\archive' # make sure destination folder exists $destinationExists = Test-Path -Path $DestinationPath if (!$destinationExists) { $null = New-Item -Path $DestinationPath -ItemType Directory } $cutoffDate = (Get-Date).AddDays(-$Days) Get-ChildItem -Path $Path -Filter $Filter -Recurse -ErrorAction SilentlyContinue | Where-Object -FilterScript { $_.LastWriteTime -lt $cutoffDate } | Move-Item -Destination c:\archive -WhatIf
The example script looks for log files with the extension *.log inside the Windows folder and all of its subfolders. Any log file older than 14 days (defined as not being modified within the past 14 days) is moved to c:\archive. This folder is created if it does not yet exist.
Note that this is only an example. You would need Administrator privileges to actually move files out of the Windows folder.