我有以下文件夹: - 10-17-17 - 10-18-17 - 10-19-17 - 10-20-17
我想让PS吐出10-20-17是我文件夹结构中的最新文件夹。我不知道从这里开始:
$path = "C:\Users\JJames\Desktop\Folder Structure Test"
$todaydate = Get-Date -Format "MM-dd-yy"
$arr = Get-ChildItem $path |
Where-Object {$_.PSIsContainer} |
Foreach-Object {$_.Name}
我能够把它吐出每个文件夹的名字。
答案 0 :(得分:2)
您应该能够对对象进行排序:
$path = "C:\Users\JJames\Desktop\Folder Structure Test"
$todaydate = Get-Date -Format "MM-dd-yy"
$folders = Get-ChildItem $path |
Where-Object {$_.PSIsContainer}
$folders | Sort-Object { [DateTime]::Parse($_.Name)} -Descending
这是简短的方法,没有日期格式的错误处理。
只获得第一项(坚持超级快速和简单)
($dateStrings | Sort{ [DateTime]::Parse($_.Name)} -Descending)[0]
答案 1 :(得分:1)
试试这个:
$path = "C:\temp"
$dirDate=New-Object DateTime
Get-ChildItem $path -Directory | %{
if ([DateTime]::TryParseExact($_.Name,
"MM-dd-yy",
[System.Globalization.CultureInfo]::InvariantCulture,
[System.Globalization.DateTimeStyles]::None,
[ref] $dirDate)
)
{
$_ | Add-Member -MemberType NoteProperty "DateName" -Value $dirDate
$_
}
} | sort DateName -Descending | select -First 1