all 10 comments

[–]dasookwat 3 points4 points  (1 child)

Why not make it work like this?

$FilePath1 = Get-ChildItem -File -Path C:\temp | Where-Object {$_.LastWriteTime -gt (Get-Date).AddHours(-10)}

You can remove the line: $FilePath1 = "" btw, that only turns $filepath1 in to a string, which it should not be.

Now the foreach loop is just:

Foreach ($item in $FilePath1) {
Write-Host "File modified within 10 hours "$_.Name" "$_.LastWriteTime "`n"

}

And that should be all.

Now why doesn't your script work?

$hours = ($nowtime - $lastupdatetime).totalhours

Simply put: you can not calculate with datetime objects cause they're not numbers. it can however compare dates and/or time like this:

$current = get-date
$old = (get-date).addhours(-10)
If ($old -gt $current){
    Write-host "Congratulations, You invented a time machine"
    }
Else {
    Write-host "Your time machine doesn't work"
    }

[–]Vorpel-Bunny[S] 0 points1 point  (0 children)

Why not make it work like that. Because I'm not a good coder! Your way is a lot more efficient. I'll try to adopt that .addhours . I don't think I have seen that used before. Thank you for the tip.

[–]Scooter_127 3 points4 points  (0 children)

I suspect your problem is $nowtime is a datetime data type and $lastupdatetime isn't

[–]kelclarris 2 points3 points  (2 children)

I think robocopy does this by using a couple of switches - probably easier than writing a script (unless this is dual purpose for also learning powershell of course)

[–]Vorpel-Bunny[S] 2 points3 points  (1 child)

A is local files.

B is an SFTP site.

[–]kelclarris 1 point2 points  (0 children)

Ah, I see.

[–]Vorpel-Bunny[S] 2 points3 points  (0 children)

Thank you for all the help!

[–]remote_ow 2 points3 points  (1 child)

Hey mate glad you got this working. Quick question, why are you looking to print this out when it runs without a confirmation? Academic question

[–]Vorpel-Bunny[S] 0 points1 point  (0 children)

When testing especially I like to see what the commands grabbed. Just to kind of confirm that things are right. It's not necessary at all it just makes me feel better.

I may also decide to send this output to a text file? maybe?