Public/Install-NuGet.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function Install-NuGet
{
    <#
        .SYNOPSIS
            Installs NuGet.exe.
        .DESCRIPTION
            Installs NuGet.exe.
        .PARAMETER Force
            A switch specifying whether or not to force the install if already installed.
        .EXAMPLE
            Install-NuGet
 
            Description
            -----------
            This example will install NuGet.exe.
        .NOTES
    #>

    [CmdLetBinding()]
    param(
        [Parameter(Mandatory = $false)]
        [switch] $Force
    )
    
    try 
    {
        $NuGetPath = Get-NuGetPath
        if (!(Test-Path $NuGetPath) -or $Force)
        {
            $ParentDirectory = Split-Path -Path $NuGetPath -Parent
            if (!(Test-Path $ParentDirectory)) { New-Item -Path $ParentDirectory -ItemType Directory -Force | Out-Null }
            (New-Object System.Net.WebClient).DownloadFile("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", $NuGetPath)
            $env:Path += ";{0}" -f $ParentDirectory
        }
    }
    catch
    {
        Write-Error $_
    }
}