Use Chocolatey to Automatically Install PC Programs!

Automatically install all your favorite programs with just one click.
Odin Odin (25)
0

Chocolatey is a PowerShell-based software management solution that allows you to manage every aspect of your Windows software environment. You just type a simple PowerShell command for whatever program you want to install and manage, run it, and boom — instant success.

Not only can Chocolatey manage the installation of software, it can automatically install your software via PowerShell!

PowerShell commands can be run through a .bat file that you set up beforehand. Don’t worry if this sounds complicated, we’re going to walk you through the creation of the commands you’ll need to install the programs you want, as well as how to create the batch file itself.

Once you’ve created this file you can store it in the Cloud or on a local device, like a flash drive. When you find yourself needing to install your software on a new computer, all you do is drop the batch file onto your new device, run it, and the PowerShell will begin to run automatically.

Notepad++

If you’re familiar with it, Notepad++ is a really powerful and simple tool that will make creating something like this batch file a whole lot easier. If you prefer to use Windows notepad you totally can, but we definitely recommend Notepad++.

Posted in these interests:
Chocolatey Logo
h/chocolatey3 guides
h/code69 guides
PowerShell Logo
h/powershell3 guides
Preparing your batch file for Chocolatey

Open a blank notepad file. This is what will eventually become your batch file. We’re going to create a fun and simple script that will make installing Chocolatey and all Chocolatey packages really easy.

Checking for Admin privileges

Copy the following lines of code into your notepad. Add each one as a new line.

echo OFF

NET SESSION >nul 2>&1

IF %ERRORLEVEL% EQU 0 (

   echo.

) ELSE (

   echo.-------------------------------------------------------------

   echo ERROR: YOU ARE NOT RUNNING THIS WITH ADMINISTRATOR PRIVILEGES.

   echo. -------------------------------------------------------------

   echo. If you're seeing this, it means you don't have admin privileges!

   pause

   echo.

   echo. You will need to restart this program with Administrator privileges by right-clicking and select "Run As Administrator"

   pause 

    echo.

   echo Press any key to leave this program. Make sure to Run As Administrator next time!

   pause

   EXIT /B 1

)

This code will tell your computer to check to see if you have Administrator privileges enabled when you run the batch file. Since Chocolatey requires admin privileges to do its thing, this will save you some trouble and confusion.

CodeExample2

Now add a blank line in notepad and add the following code to your expanding file:

powershell -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
  • Now add this on a new line:
choco feature enable -n=allowGlobalConfirmation
  • Now add the following (each on its own line!):
echo Chocolatey is ready to begin installing packages!

pause
Tell Chocolatey to automatically install

You'll want to search for the correct packages to install from the Chocolatey website. You can also install Chocolatey packages from within PowerShell but the web interface does make it easy to be certain of which package is the correct one (especially if several different packages with similar names exist). You can find the list of all Chocolatey packages here.

Add your chocolatey packages to your text file

You're doing great! Now we're going to put in the code that tells Chocolatey which packages we want to install. We're also going to leave our future self a handy reminder of where things go.

@rem ----[ Whatever you want to install, place it below this point, each item on its own line (to make it easier to find later on. ] ----

Now, this is where you will put the instruction that tells Chocolatey what you want to install. In our example below, we've chosen to install WinRaR.

choco install winrar

Now add the final lines.

@rem ---[You can add as many programs here as you desire!]----

pause
echo.
echo Your installation is complete.
pause

And you are done!

All you need to do now is save this file. Save a copy as a .txt to start with so you can edit it easily in the future. Then go to the next step to turn your .txt file into a .bat file, which will run this code as a set of instructions for your computer.

Turn your text file into a batch file

If you're using Notepad++ this step is super easy. Just select save-as from the file menu and then select ".bat". If you're using the basic Windows notepad app, however, don't worry, it's still a breeze.

  • Make sure to save your work someplace easy to find, like your desktop.
  • Open Windows File Explorer.
  • Select the "View" pane and check the box that says "File name extensions".
  • Now right-click on your newly-created text file and choose "Rename".
  • As soon as you do, the name of the file will be auto-selected. Ignore that. Look at the end of the file name, where it says .txt, and simply change that to .bat. Save and agree to any messages that Windows pops up.
Change txt to bat
  • Now, test your new batch file! If you simply double-click on it, the file will run the error script we wrote earlier reminding you to use Administrator privileges when running. To run those, just right-click and choose Run as Administrator.
RunningBat

Now, simply run your new .bat and watch the magic happen! The Chocolatey software is pretty smart, so it will handle everything once you tell it what you want.

Any errors will be displayed in yellow (such as if you had a copy of Chocolatey already installed, in which case the program will skip installing it), but otherwise, you should end up with a successful installation of your files!

Now all you need to do is place your new .bat file safely on the cloud or on an external storage drive so that you have it handy for the next time you want to hit the ground running with a new computer.

Chocolatey makes uninstalling old and unneeded software a breeze.
Odin Odin (25)
0

Have you ever found program management on a Windows system to be a bit of a pain? Well, no longer!