How to Run Shell Script in Windows

Reading time icon 7 min. read


Readers help support MSpoweruser. We may get a commission if you buy through our links. Tooltip Icon

Read our disclosure page to find out how can you help MSPoweruser sustain the editorial team Read more

Shell script files or SH files (.sh extension) can be really useful for software engineers when performing a wide variety of operations, from file processing to program execution and more. This scripting language commands file containing a series of commands is known to be used for Linux operating system by running it through a computer program called BASH. In relation to that, Bash shell scripts are written in the BASH (Bourne-Again Shell) language that is recognized by Linux programs. However, using it directly on Windows through Command Prompt or PowerShell terminal is not possible as the command language is not known to the system. Thankfully, there are a variety of ways how to run .sh or shell script files in Windows 10 and Windows 11.

Using shell script files in Linux is simpler than running them on Windows. In the latter system, you will need the use of other software in order to execute bash scripts as batch files that work for Windows systems. You can also try to convert script shell files to Windows executable BAT files, but it will require immense knowledge in commands of scripts and batch files, including translating a script file into a batch file. This option is complicated, but there are simpler ways to run shell scripts in Windows 10/11. Here are some of them.

Windows Subsystem for Linux

The first option is to use the Windows Subsystem for Linux or WSL, a compatibility layer for running Linux binary executables natively on Windows systems. It is applicable to most of the scenarios, allowing you to run a shell script in Windows 11/10.

WSL is not automatically available, so you have to enable it through the developer settings of your Windows device. You can do it by going to Settings > Update & Security > For developers. Toggle on the Developer Mode and confirm the prompt by selecting Yes. Next, look for the Windows Features (you can use the Find a Setting search bar) and choose the option “Turn Windows features on or off.” This will give you a window pop-up listing, wherein you should see the “Windows Subsystem for Linux” feature. Mark the box beside that item and click OK to install it. It will take a minute or two as the process will involve searching for relevant files for the feature and applying the changes, but it will soon ask you to restart your machine once the process is completed. After restarting, BASH should be already available in the Command Prompt and PowerShell.

After that, you need to get Ubuntu in order to access the Linux filesystem and directly run Linux commands and tools and Bash scripts or .sh files in a Linux environment on Windows. To do so, go to the Microsoft Store and download Ubuntu, which will give you the Linux distro. After that, open Ubuntu to allow it to make the installation on the hard disk and integrate with the WSL. You will then be asked to create a username and password to complete the process. You can also install a distro for Windows using the Power shell command “Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1604 -OutFile Ubuntu.appx -UseBasicParsing” or the curl command “curl.exe -L -o ubuntu-1604.appx https://aka.ms/wsl-ubuntu-1604“. Next, update and upgrade the Linux distro by downloading extra packages using the commands “sudo apt update && sudo apt upgrade.”

Once everything is settled, you can try to run shell scripts in Windows Command Prompt. Locate the script files stored as text files in your system, and right-click on an empty space inside the folder where the script files are located while pressing the Shift key. This will give you the option for “Open command prompt here” or “Open PowerShell Window here.” Select that option, type “bash” in the Command Prompt window or the PowerShell interface, and hit the Enter key to launch the distro prompt. This will bring you to the bash shell, meaning the Windows environment has changed to BASH. Confirming this is the new line that will appear in the terminal window. Finally, you can start typing the .sh file in the terminal window and just hit Enter.

Cygwin

Cygwin has a collection of GNU and Open Source tools that provide functionality similar to a Linux distribution on Windows. Using this will allow you to compile Unix or Linux applications or programs and run them on a Microsoft Windows operating system (OS) from within a Linux-like interface comparable to Linux BASH.

Cygwin is free, so you can download it anytime on Cygwin’s official website. Download and start the installation of the setup-x86_64.exe file. There are two versions of Cygwin: the 32-bit and 64-bit versions. Nonetheless, we suggest having the latter since “a full 32-bit Cygwin distro is not feasible anymore, and will in all likelihood fail in random places due to an issue with the fork system call,” as stated on its site. The 32-bit Cygwin is only for limited scenarios, including when there’s no way to run a 64-bit version of Cygwin. Once the GUI installer (which can be run to download a complete Cygwin installation) is downloaded, open it on the Downloads folder and follow the instructions on each screen to install Cygwin. Its site also has a detailed document for setting it up.

Once ready, open the Cygwin64 Terminal through the shortcut the installer will produce on the desktop. Provide the command “cd C:Users/Username/Desktop” to change the directory to where the script file is stored. After that, you can start executing shell scripts. Just type the SH file name together with the .sh extension (e.g., sh test.sh).

Git

Git is a free and open-source version control system ideal for a group of programmers working together or coordinating on a source code project. It can keep track of the changes you make to files over time, allowing you to see who made changes to a file at different times and revert files to a previous state.

Linux and macOS PCs have the pre-installed Git as their command line tool, but Git also has versions for all operating systems downloadable on its official website. In this case, you would want to have Git for Windows. Once downloaded, visit your Downloads folder and open the downloaded setup file to start the Git installation. Follow the instructions that will appear on the device screen as the installation progresses, and make sure to get the “Git bash Here” option checked when a window pop-up listing appears confirming your desired setup for the components. As for the other options for the rest of the components, just keep them checked as default. Just keep on selecting Next until you reach the Install button. Close the installation wizard and open the Git Bash by searching for it on your computer. You’ll see other Git-related apps but click for the one that says “Git Bash.”

Once launched, you need to change the directory to where your file is located by using the command “cd desktop.” Also, you can go to the folder where the SH file is stored and right-click on the folder to select the “Git Bash Here” option. When the Git command terminal or Git Bash appears, you can start putting the SH executing commands. Just type the SH file name together with the .sh extension (e.g., sh test.sh).

More about the topics: Bash, Cygwin, Git bash, linux, powershell, programming, SH files, shell script files, shell scripts, Ubuntu