Powershell Script to copy files based on date modifed to check newest file from a remote location Powershell Script to copy files based on date modifed to check newest file from a remote location powershell powershell

Powershell Script to copy files based on date modifed to check newest file from a remote location


If you want to use Hours and minutes, instead of AddDays, just use the .AddMinutes(), .AddHours(), or .AddSeconds() methods instead.

For what it's worth, I made a small modifcation, adding an Else{Scriptblock} to the script to echo out the files which aren't being copied. As written your code will only copy files written in the last 24 hours.

$RemotePath = "t:\"$LocalPath = "C:\temp"$Max_days = "-1"#Max_mins = "-5"$Curr_date = get-date#Checking date and then copying file from RemotePath to LocalPathForeach($file in (Get-ChildItem $RemotePath)){    if($file.LastWriteTime -gt ($Curr_date).adddays($Max_days))    {        Copy-Item -Path $file.fullname -Destination $LocalPath -WhatIf        #Move-Item -Path $file.fullname -Destination $LocalPath    }    ELSE    {"not copying $file"    }}>What if: Performing the operation "Copy File" on target "Item: T:\file.htm Destination: C:\temp\file.htm".not copying ListOfSacredVMs.txtnot copying newUser_01.pngnot copying newUser_015.pngnot copying newUser_02.pngnot copying newUser_03.pngWhat if: Performing the operation "Copy File" on target "Item: T:\values.csv Destination: C:\temp\values.csv".


It has been a while since I did anything in PowerShell. However you may want to try to Echo out the values you are comparing to see what they are really returning.

Echo "LastWrite : " & $file.LastWriteTime

Echo "Copy After : " & ($Curr_date).adddays($Max_days)

This would help to determine what you are attempting to compare.HopeThisHelps at least a little.