You are with your friends, you want to find the latest video recording. The recording that you made, when you were together.
How are we going to do that?
First we need to find all the files.
Get-ChildItem -File -Path .
After the first step, we can procced forward in three different ways.
Method 1:
Sort the files using Sort-Object and use -Bottom attributes to fetch the Newest file.
This is the shorter and more efficient commands than other methods. But, the other methods are good to know.
Find Newest File
Get-ChildItem -File | Sort-Object -Property CreationTime -Bottom 1;
Get-ChildItem -File -Recurse | Sort-Object -Property CreationTime -Bottom 1;
Method 2 :
Then sort the files in descending order on it's creation time.
Sort-Object -Property CreationTime -Descending
And, Select the first element.
Select-Object -First 1
Putting all of this together code looks like this.
Get-childItem -File -Path . -Recurse | Sort-Object -Property CreationTime -Descending | Select-Object -First 1
Note : If you want to get the newest file from the current folder only, then remove -Recurse .
Method 3 :
Then sort the files based on Creation time.
Sort-Object -Property CreationTime
And, Select the last object.
Select-Object -Last 1
Putting all of this together looks like this.
Get-ChildItem -File -Path . -Recurse | Sort-Object -Property CreationTime | Select-Object -Last 1
Note : If you want to get the newest file from the current folder only, then remove -Recurse.