PowerShell: Installing or Including an Application On a Computer or Scripting Session

Sample Usage:

PS C:\WINDOWS\system32> includeapp -appName notepadplusplus -appExe notepad++
notepadplusplus version 7.91.0.0 already exists.
True

function includeApp($appName,$appExe=$False,$version){
    if(!$appExe){$appExe=$appName}
    try{
        $existingExe=get-command $appExe -ea SilentlyContinue
        if(!$existingExe){
            # Include Chocolatey
            if (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) {
                [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
                Set-ExecutionPolicy Bypass -Scope Process -Force;
                iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
            }
            choco install $appName -y
            $installed=$null -ne (get-command $appExe -ea SilentlyContinue)
            return $installed
        }else{
            $existingVersion=$existingExe.Version
            write-host "$appName version $existingVersion already exists." -ForegroundColor Green
            return $true
        }
    }catch{
        write-warning $_
        return $false
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *