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.
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
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.
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.
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.
- 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.
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.