Private/Install-SpecNugetPackageProvider.ps1

Function Install-SpecNugetPackageProvider {
    <#
    .SYNOPSIS
        Installs the NuGet package provider if it is not already installed.
 
    .DESCRIPTION
        The Install-SpecNugetPackageProvider function checks if the NuGet package provider is installed on the system.
        If the provider is not found, it installs it using the Install-PackageProvider cmdlet from the PackageManagement module.
 
    .PARAMETER Scope
        Specifies the scope in which to install the NuGet package provider. Use "AllUsers" to install it for all users
        or "CurrentUser" to install it only for the current user.
 
    .VALIDATION
        Valid values for the Scope parameter are "AllUsers" and "CurrentUser".
 
    .EXAMPLE
        Install-SpecNugetPackageProvider -Scope AllUsers
        Installs the NuGet package provider for all users.
 
    .EXAMPLE
        Install-SpecNugetPackageProvider -Scope CurrentUser
        Installs the NuGet package provider for the current user.
 
    .NOTES
        Author: owen.heaume
        Version: 1.0 - initial function
    #>

    [cmdletbinding()]
    param (
        [parameter (Mandatory = $true)]
        [ValidateSet("AllUsers", "CurrentUser")]
        [String]$Scope
    )

    $nugetProvider = Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue

    if (!$nugetProvider) {
        Write-Verbose "NuGet package provider is not installed. Installing..."
        Install-PackageProvider -Name NuGet -Force -Verbose:$false -Scope $Scope
        Write-Verbose "NuGet package provider installed successfully."
        return $true
    } else {
        Write-Verbose "NuGet package provider is already installed."
        return $false
    }
}