PowerShell: Use Chocolatey to Install Apps on Windows (Similar to Apt-Get or Yum)

Quick installation paste:

# Install 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'))}

# Defining $ENV:ChocotaleyInstall so that it would be called by refreshenv
$ENV:ChocolateyInstall = Convert-Path "$((Get-Command choco).Path)\..\.."
Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
Update-SessionEnvironment

# Install commonly used packages
$packages = 'powershell','googlechrome','notepadplusplus.install','adobereader','7zip.install','javaruntime','vlc','putty.install'
ForEach ($package in $packages){
choco install $package --ignore-checksums -y
}

This error would occur on Windows 2008 if TLS 1.2 hasn’t been enabled. Follow this link to fix it: https://kimconnect.com/powershell-enable-tls-1-2-on-legacy-servers/

Exception setting "SecurityProtocol": "Cannot convert null to type "System.Net.SecurityProtocolType" due to invalid enu
meration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Ss
l3, Tls"."
At line:2 char:28
+ [Net.ServicePointManager]:: <<<< SecurityProtocol = [Net.SecurityProtocolType]::Tls12
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
# Deprecate instructions...
<# Preemtively resolve this error
Exception calling "DownloadString" with "1" argument(s): "The underlying connection was closed: Could not establish
trust relationship for the SSL/TLS secure channel."
At line:3 char:51
+ ... ess -Force; iex ((New-Object System.Net.WebClient).DownloadString('ht ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
#>

add-type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12,Tls13'
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
# Install commonly used packages
$packages = 'powershell','googlechrome','firefox', 'git.install', 'notepadplusplus.install','atom','adobereader','7zip.install','javaruntime','vlc','putty.install','sysinternals','transmission','mRemoteNG'
ForEach ($package in $packages)
{
choco install $package -y
}

Output:

Getting latest version of the Chocolatey package for download.
Getting Chocolatey from https://chocolatey.org/api/v2/package/chocolatey/0.10.13.
Downloading 7-Zip commandline tool prior to extraction.
Extracting C:\Users\KimConnect\AppData\Local\Temp\chocolatey\chocInstall\chocolatey.zip to C:\Users\KimConnect\AppData\Local\Temp\chocolatey\chocInstall...
Installing chocolatey on this machine
Creating ChocolateyInstall as an environment variable (targeting 'Machine')
Setting ChocolateyInstall to 'C:\ProgramData\chocolatey'
WARNING: It's very likely you will need to close and reopen your shell
before you can use choco.
Restricting write permissions to Administrators
We are setting up the Chocolatey package repository.
The packages themselves go to 'C:\ProgramData\chocolatey\lib'
(i.e. C:\ProgramData\chocolatey\lib\yourPackageName).
A shim file for the command line goes to 'C:\ProgramData\chocolatey\bin'
and points to an executable in 'C:\ProgramData\chocolatey\lib\yourPackageName'.

Creating Chocolatey folders if they do not already exist.

WARNING: You can safely ignore errors related to missing log files when
upgrading from a version of Chocolatey less than 0.9.9.
'Batch file could not be found' is also safe to ignore.
'The system cannot find the file specified' - also safe.
chocolatey.nupkg file not installed in lib.
Attempting to locate it from bootstrapper.
PATH environment variable does not have C:\ProgramData\chocolatey\bin in it. Adding...
WARNING: Not setting tab completion: Profile file does not exist at
'C:\Users\KimConnect\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'.
Chocolatey (choco.exe) is now ready.
You can call choco from anywhere, command line or powershell by typing choco.
Run choco /? for a list of functions.
You may need to shut down and restart powershell and/or consoles
first prior to using choco.
Ensuring chocolatey commands are on the path
Ensuring chocolatey.nupkg is in the lib folder Chocolatey v0.10.13
2 validations performed. 1 success(es), 1 warning(s), and 0 error(s).

Validation Warnings:
- A pending system reboot request has been detected, however, this is
being ignored due to the current Chocolatey configuration. If you
want to halt when this occurs, then either set the global feature
using:
choco feature enable -name=exitOnRebootDetected
or pass the option --exit-when-reboot-detected.

Installing the following packages:
googlechrome
By installing you accept licenses for the packages.
Progress: Downloading chocolatey-core.extension 1.3.3... 100%
Progress: Downloading GoogleChrome 74.0.3729.169... 100%

chocolatey-core.extension v1.3.3 [Approved]
chocolatey-core.extension package files install completed. Performing other installation steps.
Installed/updated chocolatey-core extensions.
The install of chocolatey-core.extension was successful.
Software installed to 'C:\ProgramData\chocolatey\extensions\chocolatey-core'

GoogleChrome v74.0.3729.169 [Approved]
googlechrome package files install completed. Performing other installation steps.
Google Chrome 74.0.3729.169 is already installed.
The install of googlechrome was successful.
Software install location not explicitly set, could be in package or
default install location if installer.

... omitted for brevity ...

How to remove package

PS C:\Windows\system32> choco uninstall skype
Chocolatey v0.10.13
2 validations performed. 1 success(es), 1 warning(s), and 0 error(s).

Validation Warnings:
- A pending system reboot request has been detected, however, this is
being ignored due to the current Chocolatey configuration. If you
want to halt when this occurs, then either set the global feature
using:
choco feature enable -name=exitOnRebootDetected
or pass the option --exit-when-reboot-detected.

Uninstalling the following packages:
skype

skype v8.45.0.41
Running auto uninstaller...
Auto uninstaller has successfully uninstalled skype or detected previous uninstall.
skype has been successfully uninstalled.

Chocolatey uninstalled 1/1 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
# Possible Error:
Invoke-WebRequest : Powered By FortinetFortiGuard Web FilteringblockedWeb Page Blocked!
You have tried to access a web page which is in violation of your internet usage policy.
URL: https://chocolatey.org/
Category: Information Technology
User name:
Group name:
To have the rating of this web page re-evaluated please click here.
At line:2 char:56
+ ... Force; iex (Invoke-WebRequest -Uri 'https://chocolatey.org/install.ps ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Leave a Reply

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