How to get latest binary version from Archieve using Powershell

I need a PowerShell script to get latest binary version from this archive list https://download.docker.com/win/static/stable/x86_64/

So as when I run the script, it checks for latest zip and returns me latest version number and link to download. For example, if latest file is docker-24.0.6.zip then I need

  • 24.0.6
  • link to that zip which ultimately I can use to download

I have subsequent script to utilize that zip but I have never tried getting it from an archive url.

>Solution :

docker-check.ps1:

$archiveUrl = "https://download.docker.com/win/static/stable/x86_64/"

$response = Invoke-WebRequest -Uri $archiveUrl

$pattern = '<a href="([^"]*docker-([\d.]+)\.zip)"'
$matches = [regex]::Matches($response.Content, $pattern)

$latestVersion = $null
$latestDownloadLink = $null

foreach ($match in $matches) {
    $downloadLink = $match.Groups[1].Value
    $version = $match.Groups[2].Value

    if ($latestVersion -eq $null -or [Version]$version -gt [Version]$latestVersion) {
        $latestVersion = $version
        $latestDownloadLink = $downloadLink
    }
}

if ($latestVersion -ne $null -and $latestDownloadLink -ne $null) {
    Write-Host "Latest Version: $latestVersion"
    Write-Host "Download Link: $archiveUrl$latestDownloadLink"
} else {
    Write-Host "Unable to find the latest version and download link."
}

Output:

enter image description here

Leave a Reply