Skip to content

Instantly share code, notes, and snippets.

@bentodaniel
Last active October 5, 2023 20:34
Show Gist options
  • Save bentodaniel/46b92d8d0d9fc67edb175e67a85d4da0 to your computer and use it in GitHub Desktop.
Save bentodaniel/46b92d8d0d9fc67edb175e67a85d4da0 to your computer and use it in GitHub Desktop.
Batch scripts using Chocolatey to quickly install windows packages
:: This script installs Chocolatey and some programs
@echo off
cls
set $choco_install=Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
set $program_install=choco install
set $upgrade_install=choco upgrade all
set $programs=googlechrome git vscode sublimetext3 discord k-litecodecpackfull
:choice
set /P c=Install Chocolatey or Install/Upgrade packages [Y/N]?
if /I "%c%" EQU "Y" goto :install
if /I "%c%" EQU "N" goto :exit
echo Error Invalid choice
goto :choice
:install
:: Checks if choco is a recognized command
WHERE choco >nul
if %ERRORLEVEL% neq 0 echo choco not installed yet & goto :install_choco
echo choco already installed & goto :install_upgrade_programs
:: Installs choco
:install_choco
echo.
echo Installing Chocolatey...
for /f "delims=" %%a in ('powershell -command Get-ExecutionPolicy') do set "$policy_value=%%a"
echo Get-ExecutionPolicy : %$policy_value%
if %$policy_value%==Restricted start powershell -Command "&{ Start-Process powershell -ArgumentList '-command Set-ExecutionPolicy AllSigned' -Verb RunAs}"
:: Resolves a problem of '' inside ''
set $choco_install=%$choco_install:'=''%
powershell -Command "&{ Start-Process powershell -ArgumentList '-command %$choco_install%' -Verb RunAs}"
goto :install_upgrade_programs
:: Installs or Upgrades programs from choco
:install_upgrade_programs
echo.
:programs_operation_choice
choice /c IU /n /t 10 /d I /m "[I]nstall or [U]pgrade packages [I/U]? "
if errorlevel 2 goto :upgrade_programs
if errorlevel 1 goto :install_programs
:install_programs
echo.
echo Predefined packages : %$programs%
echo.
:choice_progrs
echo You have 10 seconds to answer. Default is No.
choice /c YN /n /t 10 /d N /m "Do you want to install other programs [Y/N]? "
if errorlevel 2 goto :keep_programs
if errorlevel 1 goto :add_programs
:: Allows the user to install a custom list of programs
:add_programs
echo.
echo Custom package list
echo Insert the list of packages you want to install.
echo Warning you have to manually add the default packages if you want any of them
echo Find more here https://community.chocolatey.org/packages
echo.
set /p $programs=Enter list of packages (separate with space):
goto :install_programs_install
:: Constinues the instalation with the default programs
:keep_programs
echo.
echo No other programs will be installed
goto :install_programs_install
:: Installs the programs selected
:install_programs_install
echo.
echo Installing Packages...
powershell -Command "&{ Start-Process powershell -ArgumentList '-command %$program_install% %$programs% -y' -Verb RunAs}"
goto :exit
:: Upgrades all programs
:upgrade_programs
echo.
echo Upgrading All Packages...
powershell -Command "&{ Start-Process powershell -ArgumentList '-command %$upgrade_install% -y' -Verb RunAs}"
goto :exit
:: Stops execution of the script
:exit
echo.
echo Exiting...
goto :eof
:: Extras
:: color pallette : https://gist.githubusercontent.com/mlocati/fdabcaeb8071d5c75a2d51712db24011/raw/b710612d6320df7e146508094e84b92b34c77d48/win10colors.cmd
:: chocolatey : https://community.chocolatey.org/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment