How to Create & Run Shell Scripts on Windows [Step-By-Step]

You will need to enable the Windows Linux Subsystem before creating a script

Reading time icon 3 min. read


Readers help support Windows Report. When you make a purchase using links on our site, we may earn an affiliate commission. Tooltip Icon

Read the affiliate disclosure page to find out how can you help Windows Report effortlessly and without spending any money. Read more

Key notes

  • A shell script can help carry out repetitive automated tasks such as backup and software installations via the command line. 
  • It is possible to run a shell script file in Windows using the Windows Subsystem for Linux.
shell script for windows

A shell script is a shell programming language program typically used on Linux and another Unix-based operating system. However, you can create and run shell scripts on Windows with additional steps. 

Here in this guide, we have mentioned a step-by-step method to run a shell script on Windows. Let’s get started!

How can I create and run shell scripts on Windows?

1. Install the Windows Subsystem

  1. Press Windows + I to open Settings.
  2. Go to Privacy & Security, and click For developersSettings - Privacy Security
  3. Click the toggle switch next to Developer mode. Now click Yes on the Use developer features window.Developer settings
  4. Press the Windows button and type Turn Windows features. Next, select the option displayed and click OpenWindows features - shell script for windows
  5. Locate Windows Subsystem Linux and place a checkmark next to it. Click OK.Windows Subsystem for Linux
  6. Follow the onscreen instructions and restart your PC to finish. 

2. Download Ubuntu & Integrate it with WSL

  1. Press the Search button and type Microsoft Store.
  2. Launch the store app and type Ubuntu in the search bar.Ubuntu -shell script for windows
  3. Click Ubuntu from the search and click on Get to download it.
  4. Search for the app and launch it.
  5. The Linux command prompt will install on your disk.Ubuntu - shell script for windows
  6. The integration with WSL will also initiate.
  7. Enter the username and password and note it down for future use. The process might take some time to complete. 

3. Create a shell script

  1. Go to the desired location on your computer, press the Shift key, and right-click on the blank space.Powershell  -shell script for windows
  2. From the context menu, click the Open PowerShell window here.
  3. Type the following command to get redirected to the bash shell: bashBash
  4. Next, copy and paste the following command after replacing the filename with the name of your file and press Enter to create a shell script file: cat>filename.shcatscript - create script
  5. Check the created file and go back to PowerShell. Press Ctrl + C to terminate the process. 
  6. To run the shell script, you need to add some data. On the PowerShell window, type the following command after replacing the filename with the name of the file and press Enter. This command will open a file for editing: vi filename.shvi script1.sh
  7. Press the I key to access the file and start editing it. 
  8. Type the following command in the file: echo “Hello World”Enter -shell script for windows
  9.  To save the contents of the file and close it, press the Esc key from the keyboard and type the following command, and press Enter: wq 

4. Run the Shell Script

4.1 Use the PowerShell

  1. Go to the location where your sh file is present, press the Shift key, and right-click the blank space. 
  2. From the context menu, click the Open PowerShell window here
  3. Type the following command and press Enter to go to the bash shell: bash
  4. Next, copy and paste the following command and press Enter: sh filename.shHello world output -Enter -shell script for windows
  5. You will see the output. 

4.2 Use Command Prompt

  1. Press the Windows key, type CMD and click Open.
  2. Type the following command and replace the E:/New folder with the path of your file, and press Enter: cd /d E:/New folder.
  3. Next, copy and paste the following command, replace the filename with the name of the file, and press Enter: bash filename.shBash output cmd -Hello world output -Enter -shell script for windows
  4. You will get output on your command prompt window. 

 So, these are steps that you can follow to run shell script commands on Windows. Try this step-by-step guide and if you have any questions, let us know in the comments section below.

More about the topics: PowerShell, windows 10, Windows 11