Public/Install-SpecNuGetVersion.ps1

function Install-SpecNuGetVersion {
    <#
    .SYNOPSIS
    Installs the specified NuGet version if not already installed.
 
    .DESCRIPTION
    This function checks if the specified NuGet version is installed. If the installed version is greater than or equal to the required version, it displays a message indicating that the required version is already installed. If the installed version is lower than the required version, it attempts to install the required version of NuGet.
 
    .PARAMETER requiredVersion
    The version of NuGet to be installed. Default value is '2.8.5.208'.
 
    .EXAMPLE
    Install-SpecNuGetVersion -requiredVersion '3.4.3'
    Installs NuGet version '3.4.3' if not already installed.
 
    .NOTES
    Author : owen.heaume
    Version : 1.0
 
    #>


    param (
        [string]$requiredVersion = '2.8.5.208'
    )

    Write-Host "Checking if NuGet version [$requiredVersion] or higher is installed" -ForegroundColor DarkCyan

    $nugetVersion = (Get-PackageProvider NuGet -ListAvailable | Where-Object { $_.Name -eq 'NuGet' }).Version

    if ($nugetVersion -ge [version]$requiredVersion) {
        Write-Host "NuGet is already installed." -ForegroundColor DarkGray
    } else {
        Write-Host "NuGet version $nugetVersion is installed. Installing version [$requiredVersion]" -ForegroundColor DarkGray
        try {
            Install-PackageProvider NuGet -MinimumVersion $requiredVersion -Force -ea Stop -ev x
        } catch {
            Write-Host "Unable to install Nuget. The error was: $x" -ForegroundColor DarkGray
        }
    }
}