In my folder (Test) I have multiple builds (Test_6.6.80.x). I want to get the latest build.
This
$Directory = 'C:\Test'
$Latest = (Get-ChildItem -Path $Directory).Name | Sort-Object { [version] } | Select-Object -Last 1
Returns TEST_6.6.80.12
because the sorting looks like this
TEST_6.6.80.16
TEST_6.6.80.15
TEST_6.6.80.6
TEST_6.6.80.5
TEST_6.6.80.14
TEST_6.6.80.11
TEST_6.6.80.10
TEST_6.6.80.13
TEST_6.6.80.12
How do I fix the sorting?
CodePudding user response:
Try this.
$last = Get-ChildItem -Path $Directory | Select-Object Name, @{l='Version';e={[System.Version]::Parse($_.Name.Split('_')[1])}} | Sort-Object Version | Select-Object -Last 1
$last.Name
CodePudding user response:
try this:!
$root='C:\Test'
Get-ChildItem $root -Directory -filter "TEST_*.*.*.*" | %{
$version=[System.Version]::Parse(($_ -split '_')[1])
Add-Member -InputObject $_ -NotePropertyName "Version" -NotePropertyValue $version -PassThru
} | sort Version -Descending | select FullName, Version -First 1