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

PowerShell: Error when Pause is Placed Inside Function

Error: + Function Search-ScheduledTasks{+ ~Missing closing '}' in statement block or type definition.At C:\Users\kdoan\Desktop\Notes\test.ps1:57 char:1+…

PowerShell: How to Play a Sound

$soundFile="C:\Windows\media\tada.wav"$sound = new-Object System.Media.SoundPlayer$sound.SoundLocation=$soundFile$sound.Play()

PowerShell: Set Application CPU Affinity on Remote and Local Computers

# setProcessNameToCpuNumber.ps1$computerName =$env:computername$processname="Chrome";function getProcessors{ param( $computerName=$env:computername ) [int]$processors = 0; $cpuObject = Get-WmiObject -computername $computerName…