Welcome › Forums › General PowerShell Q&A › Last file in folder, wrong output
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.
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
Can we see the code – please
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
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.