Public/Install-SpecPowerShellModule.ps1

function Install-SpecPowerShellModule {
    <#
    .SYNOPSIS
        Installs and updates specified PowerShell modules from the PowerShell Gallery.
 
    .DESCRIPTION
        The Install-SpecPowerShellModule function allows you to install and update specified PowerShell modules from the
        PowerShell Gallery. It checks if the module is already installed and, if required, installs the specified version
        or the latest version available. The function also supports automatically accepting license agreements using the
        -AcceptLicense switch.
 
    .PARAMETER ModuleName
        Specifies the name of the PowerShell module(s) to install or update. This parameter supports pipeline input.
 
    .PARAMETER Scope
        Specifies the scope in which to install the PowerShell module(s). Use "AllUsers" to install them for all users
        or "CurrentUser" to install them only for the current user.
 
    .VALIDATION
        Valid values for the Scope parameter are "AllUsers" and "CurrentUser".
 
    .PARAMETER RequiredVersion
        Specifies the required version of the PowerShell module(s) to install. If this parameter is provided, the function
        will attempt to install the specified version of the module(s). If not provided, it will install the latest available version.
 
    .PARAMETER AcceptLicense
        Indicates whether to accept the license agreement for the module(s) automatically. If this switch is used, the function
        will not prompt the user for acceptance.
 
    .EXAMPLE
        "ExampleModule" | Install-SpecPowerShellModule -Scope AllUsers -RequiredVersion "1.2.0" -AcceptLicense
        Installs version 1.2.0 of the "ExampleModule" PowerShell module for all users, automatically accepting the license.
 
    .EXAMPLE
        "AnotherModule" | Install-SpecPowerShellModule -Scope CurrentUser
        Installs the latest version of the "AnotherModule" PowerShell module for the current user.
 
    .EXAMPLE
        $Modules = @(
            @{
                moduleName = 'SpecBaseModule'
                requiredVersion = "1.0.2"
 
            },
            @{
                moduleName = 'Az.Accounts'
                requiredVersion = '2.10.2'
            }
        )
 
        $modules | % { Install-SpecPowerShellModule -Module $_.moduleName -RequiredVersion $_.requiredVersion -Scope CurrentUser -Verbose}
        Installs version 1.0.2 of the "SpecBaseModule" and 2.10.2 "Az.Accounts" PowerShell modules for the current user.
 
    .EXAMPLE
        $Modules = @(
            @{
                moduleName = 'SpecBaseModule'
                requiredVersion = ""
 
            },
            @{
                moduleName = 'Az.Accounts'
                requiredVersion = '2.10.2'
            }
        )
 
        $modules | % { Install-SpecPowerShellModule -Module $_.moduleName -RequiredVersion $_.requiredVersion -Scope CurrentUser -Verbose}
        Installs the latest version of the "SpecBaseModule" and version 2.10.2 "Az.Accounts" PowerShell modules for the current user.
 
 
    .NOTES
        Author: owen.heaume
        Version: 1.0 - initial function
    #>

    [cmdletbinding()]
    param (
        [parameter(Mandatory = $true, Position = 1, ValueFromPipeline = $true)]
        $ModuleName,

        [Parameter(Mandatory = $false)]
        [ValidateSet('AllUsers', 'CurrentUser')]
        $Scope = 'AllUsers',

        [Parameter(Mandatory = $false)]
        $RequiredVersion = "",

        [switch]
        $AcceptLicense
    )

    process {
        foreach ($module in $ModuleName) {

            write-host "`n"
            Write-Verbose "`Checking module: $module"
            # Check to see if the module is installed on the local computer - returns either false (module not installed) or the version number
            if ($RequiredVersion -ne "" -or $RequiredVersion -ne $null) {
                $installedVersionNumber = Get-SpecInstalledModule -module $Module -version $RequiredVersion -verbose

            } else {
                $installedVersionNumber = Get-SpecInstalledModule -module $Module -verbose
            }

            if ($installedVersionNumber -ne $false) {$moduleInstalled = $true}

            if ($moduleInstalled) {
                # Required version is already the same as the installed version
                if ($RequiredVersion -eq $installedVersionNumber) {
                    Write-Warning "The required version ($RequiredVersion) is the same as the installed version ($installedVersionNumber). Nothing to update."
                    $module
                    continue #continue to next module in the pipeline
                }
            }

            # Get the latest version from PowerShell gallery
            $LatestPSGalVersion = Get-SpecPSGalleryLatestVersion -Module $module -Verbose

            if ($LatestPSGalVersion -eq $false) {
                Write-Warning "The module $module cannot be found in the PowerShell gallery. Please make sure it exists or that you haven't made a typo."
                continue #continue to next module in the pipeline
            }

            if ($moduleInstalled) {
                # Required version not specified but the installed version is already the same as the latest PSGal version
                if ($RequiredVersion -eq "" -or $requiredversion -eq $null -and ($installedVersionNumber -eq $LatestPSGalVersion)) {
                    Write-Warning "The installed module version is the same as the latest version in the PowerShell Gallery. Nothing to update."
                    break
                }
            }

            # No specific version has been requested
            if ($RequiredVersion -eq "" -or $RequiredVersion -eq $null) { # No specific version has been requested
                #install NuGet package provider if not already installed
                Install-SpecNugetPackageProvider -Scope $scope -Verbose

                #install the latest version of the module from PowerShell Gallery
                if ($AcceptLicense) {
                    Write-Verbose "Installing $module version $LatestPSGalVersion"
                    Install-SpecModule -Module $module -Scope $scope -AcceptLicense -Verbose
                    #continue #continue to next module in the pipeline
                } else {
                    Write-Verbose "Installing $module version $LatestPSGalVersion"
                    Install-SpecModule -Module $module -Scope $scope -Verbose
                }
            }

            # The module is not installed - but a specific version has been requested
            if ($RequiredVersion -ne "" -or $RequiredVersion -eq $null) { # A specific version has been requested
                #install NuGet package provider if not already installed
                Install-SpecNugetPackageProvider -Scope $scope -Verbose

                #install the required version of the module from PowerShell Gallery
                if ($AcceptLicense) {
                    Write-Verbose "Installing $module version $RequiredVersion"
                    Install-SpecModule -Module $module -Scope $scope -RequiredVersion $RequiredVersion -AcceptLicense -Verbose
                } else {
                    Write-Verbose "Installing $module version $RequiredVersion"
                    Install-SpecModule -Module $module -Scope $scope -RequiredVersion $RequiredVersion -Verbose
                }
            }

            $module
        }
    }
}