Posted On December 4, 2020

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

kimconnect 0 comments
blog.KimConnect.com >> Codes >> 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 *

Related Post

SQL: Use PowerShell to Validate if Database Exists

This function may be invoked remotely from a machine that has SQL-PS module installed. The…

Deploying LDAP / Active Directory Self Service Password Portal

Overview: There are several choices of platforms to deploy Password Manager: Kubernetes, Docker, Windows, and…

PowerShell: Add Local Group Members Onto a Server List

Sometimes, the GUI method of accomplishing tasks is too arduous and error prone. Thus, these…