Link to home
Create AccountLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Change PowerShell script to move all files to Archive if older than 20 days

I would like to change the following PowerShell script to move all files regardless of extension, or no extension, if they are older than 20 days they need to be moved to the Archive folder. 

Any assistance is appreciated.

$sourcePath = "C:\Users\Users1\Inbound"
$archivePath = "C:\Users\Users1\Inbound\Archive" Get-ChildItem -Path $sourcePath -File | ForEach-Object {     $csvFile = $_     if ($csvFile.Extension -eq ".csv") {         $prefix = $csvFile.BaseName         $matchingFiles = Get-ChildItem -Path $sourcePath -Filter "$prefix*"         $duplicateFileFound = $false         foreach ($file in $matchingFiles) {             if ($file.Name -ne $csvFile.Name) {                 $duplicateFileFound = $true                 break             }         }         if ($duplicateFileFound) {             Move-Item -Path $csvFile.FullName -Destination $archivePath         }     } }

Open in new window

Avatar of oBdA

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account