Hello all
Need some help please. I need to retrieve the contents of various folders and then check the lastWriteTime. If it is greater than 14 days old I need to move the item into an archive folder. I am getting stuck on comparing the dates and then moving the files. This is what I currently have
$date = get-date
$PathToGo = “F:\Scripts\Paygate\Archive”
write-host “Now connecting to remote storage location”
start-sleep 3
#new-psdrive -name PayGate -psProvider FileSystem -Root F:\Scripts\Paygate
Get-PSDrive -name PayGate -PSProvider FileSystem
[int]$Count = 0
$Location = (get-psdrive Paygate).root
$SearchFolders = Get-ChildItem F:\Scripts\Paygate -Recurse | ForEach {$_.FullName}
$PaymentList = $SearchFolders
[int]$TotalPayment = ($SearchFolders | Measure-object).count
write-host “There are a total of $TotalPayment found.”
$lastwriteTime = (Get-Item $PaymentList).LastWriteTime
write-host $lastWriteTime
Compare-Object $b $date
ForEach ($Payment in $PaymentList)
{
copy-item $payment $PathToGo
}
donj
November 13, 2013, 4:30am
2
If you cast a date time string as an actual datetime object, you can compare them.
[datetime]$x = “11/22/1971 10:45 AM”
$y = Get-Date
if ($x -gt $y) {
#whatever
}
I believe the date you’re looking at on the file object is already a date/time, so you should just be able to do a straight comparison.
Maybe this helps:
To get a variable “from now minus 14 days” :
$x = (get-date).adddays(-14)
Maybe this helps
I know this post is old but I seem to be getting a strange result when comparing two dates.
$a = “12/18/2015”
$b = (get-date).adddays(-90) #would be 10/3/15 using today’s date of 12/31/15
if($a -gt $b){$a}else{$b}
returns $a.
Why? Shouldn’t it return $b as 12/18/15 is not ‘older’ than 10/3/15? I’m looking for accounts last modified 90 days from today.
works for me:
$sampledate = Get-Date "18.12.2015"
$nowminus90 = (Get-Date).adddays(-90) #would be 10/3/15 using today's date of 12/31/15
if($sampledate -gt $nowminus90){"sampledate: $sampledate"}else{"now: $nowminus90"}
returns: sampledate: 12/18/2015 00:00:00