functions/Install-PsModuleFast.ps1

Function Install-PsModuleFast {
    [CmdletBinding()]
    param([string] $module
        , [version] $version)
    Write-Verbose "Loading module $Module"

    Repair-PSModulePath;
    
    if (-not (get-module $module -ListAvailable | Where-object Version -gt $version)) {
        Write-Output " Installing module $module"
        if ($null -eq $version) {
            install-module $module -force -AllowClobber -Scope CurrentUser -SkipPublisherCheck
        }
        else {
            install-module $module -force -AllowClobber -Scope CurrentUser -SkipPublisherCheck -MinimumVersion $version 
        }
    }

    if (-not (get-module $module | Where-object Version -GE $version)) {
        Write-Output " importing module $module $version"
        if ($null -eq $version ) {
            import-module $module -force  
        }
        else {
            import-module $module -force  -MinimumVersion $version 
        }
    }
}

# Function Install-PsModuleFast {
# [CmdletBinding()]
# param([string] $module
# , [version] $version
# , [parameter(Mandatory=$false)][string] $path)
#
# Repair-PSModulePath;
#
# Write-Verbose "Loading module $Module"
# if (-not (get-module $(join-path $path $module) -ListAvailable | Where-object Version -ge $version)) {
# Write-Output " Installing module $module"
# if ($null -eq $version) {
# install-module $module -path $path -force -AllowClobber -Scope CurrentUser -SkipPublisherCheck
# }
# else {
# install-module $module -path $path -force -AllowClobber -Scope CurrentUser -SkipPublisherCheck -MinimumVersion $version
# }
# }
#
# if (-not (get-module $module | Where-object Version -GE $version)) {
# Write-Output " importing module $module $version"
# if ($null -eq $version ) {
# import-module $module -force
# }
# else {
# import-module $module -force -MinimumVersion $version
# }
# }
# }

function Repair-PSModulePath {
    $PSModulePath = $env:PSModulePath;
    $mydocsPath = "$([environment]::GetFolderPath("MyDocuments"))\WindowsPowerShell\Modules"
    $mya = "$PSModulePath".Split(";")
    If (-not ($mya -contains $mydocsPath)) {
        $env:PSModulePath = "$mydocsPath;$PSModulePath"
        }
}



# $PSModules = @{Module = "Pester"; Version = "4.5" }, `
# @{Module = "Microsoft.PowerApps.PowerShell" }, `
# @{Module = "Microsoft.PowerApps.Administration.PowerShell" }, `
# @{Module = "VSSetup" }
# foreach ($Ps in $PSModules) {
#
# Install-PsModuleFast @PS
# }