Get the newest file from a folder
- ToddimusPrime
- May 14, 2021
- 1 min read
I recently had to come up with a script to automate the SFTPing of a file every morning. The file that needed to be sent on its way would appear in its folder overnight right next to all of the previous files. I needed a way to get just the newest file from that folder, but I had no way of knowing what its filename would be. Here's what I did.
$folder = "<PUT YOUR FOLDER PATH HERE>"
$theFile = Get-ChildItem $folder | Sort-Object -Descending -Property LastWriteTime | Select -First 1 #gets the most recent file within $folder
$filePath = $theFile.fullname #gets the full path of #theFileThe rest of the automation for the SFTP process was done with the Posh-SSH module. More info on that can be found here.




Comments