Author Posts

August 6, 2015 at 5:19 am

When I perform this command on the command line, I receive the correct response

$Outputfiles = Get-ChildItem 'D:\TST\log\input' -Recurse | where-object LastWriteTime | select -First 1

When I do this in a script I always receive the same result, even there are new files.

Any idea?

August 6, 2015 at 5:27 am

I'm a little puzzled why you're using where-object. I'd have thought if you wanted the last file in the folder you would sort on LastWriteTime rather than just test it exists

August 6, 2015 at 5:44 am

Can we see the code – please

August 6, 2015 at 7:04 am

It seems the OP has assumed that the LastWriteTime property means "this is the last file that was written to in the folder." If you just want the most recently written file, use Sort-Object instead of Where-Object:

Get-ChildItem 'D:\TST\log\input' -Recurse -File |
    sort lastwritetime -Descending |
    select -First 1

August 6, 2015 at 11:59 pm

thank you! the problem was caused due to the fact that after I moved the file from another location the modify time was not changed.