Author Posts

November 13, 2013 at 7:32 am

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
}

November 13, 2013 at 10:00 am

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.

November 25, 2013 at 5:19 am

Maybe this helps:
To get a variable "from now minus 14 days" :

$x = (get-date).adddays(-14)

Maybe this helps

December 31, 2015 at 1:48 pm

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.

December 31, 2015 at 2:53 pm

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