Reputation: 729
i am using following powershell script for getting folder with latest time stamp
$drive = get-psdrive |select root |Select-String -InputObject {$_.Root} -Pattern ':'
Write-Host $drive
foreach ($a in $drive)
{
Get-ChildItem -Path $a -Filter "*sysout" -Recurse -Force -ErrorAction SilentlyContinue|select -last 1
}
}
it gives following output
A:\ C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\ K:\ Z:\
Directory: E:\utility
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2/21/2018 2:06 AM sysout
Directory: F:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2/21/2018 3:15 AM sysout
i need latest timestamp folder but using -last 1 doesnt give desired results.
Upvotes: 0
Views: 287
Reputation: 17161
Last
doesn't really have any meaning without order.
Get-ChildItem -Path $a -Filter "*sysout" -Recurse -Force -ErrorAction SilentlyContinue |
Sort-Object LastWriteTime -Descending |
Select-Object -Last 1
Upvotes: 1