How to install MSI from Batch Script

Written by Horatiu Vladasel · March 29th, 2023

Batch scripting is still widely used within organizations all over the world, despite the popularity of PowerShell, which is a more powerful programming language. Almost every task or action can be executed by a sequence of commands typed on the Windows Command Prompt.

In this article, we’ll see what a batch script is, how to create one, and how to use it to install an MSI.

What is a Batch Script?

A Batch Script is a sequence of commands that the Command Line interpreter runs one line at a time.

Batch Scripting comes with two filename extensions:

  • .bat - used in DOS and earlier versions of Windows, up until Windows 98
  • .cmd - added on top of .bat for IBM OS/2 and Windows NT OS.

Installing applications and setting them up is one of the most common tasks for an IT Professional.

Writing commands frequently can be a very tedious task, so creating a Batch Script file is definitely the preferred solution.

Let’s see how we can create a Batch Script to install an MSI based on OS architecture.

How to Create a Batch Script?

For today’s article, we downloaded both x64 and x86 bit MSI versions of 7Zip.

To install an MSI, we can use the START command followed by /WAIT parameter – to wait for the MSI installation to complete.

Installing a x64 bit MSI using Batch Scripting

start /wait msiexec.exe /i "%~dp07z2201-x64.msi" /qn /l*v "%SystemRoot%\Logs\7z2201-x64.log"

The %~dp0 variable expands to the drive letter and path of the batch file. And therefore the Batch Script must be located next to the MSI.

NoteCheck out our Msiexec.exe Command Line article for more information about msiexec install options.

Installing a x86 bit MSI using Batch Scripting

We’ll take a similar approach for the x86 bit MSI.

start /wait msiexec.exe /i "%~dp07z2201-x86.msi" /qn /l*v "%SystemRoot%\Logs\7z2201-x86.log"

Now, we need to check for the processor architecture and for that we can use %PROCESSOR_ARCHITECTURE% environment variable. Here’s how the code should look like:

IF "%PROCESSOR_ARCHITECTURE%"=="AMD64" (start /wait msiexec.exe /i "%~dp07z2201-x64.msi" /l*v "%SystemRoot%\Logs\7z2201-x64.log") ELSE (start /wait msiexec.exe /i "%~dp07z2201.msi" /l*v "%SystemRoot%\Logs\7z2201-x86.log").

And that’s it.

Now, you can use your Batch Script to install 7Zip based on your OS architecture. You can do that manually by double-clicking on the Batch Script. Or, you can import the Batch Script into your deployment tool along with the two MSIs and deploy it where it is needed.

Conclusion

While PowerShell may be a more powerful programming language, Batch scripting remains widely used by organizations for its ability to execute tasks and actions through a sequence of commands on the Windows Command Prompt.

However, the only commands you can use are the commands available in the Command Prompt.

This method helps you with simple automation tasks, but if you deal with more complex tasks, we recommend you look into PowerShell.

Did you find this short tutorial useful?

Check out this video tutorial to learn how to install and run a CMD or BAT.

Written by
See author's page
Horatiu Vladasel

Horatiu is a Software Packager/Sequencer with over 10 years experience, who has worked as a Software Packager at IBM and is currently offering software packaging services to companies such as BT or Nationwide.

Comments: