Running Shell Programs in Windows: A Comprehensive Guide
Introduction
Running shell programs in Windows is a fundamental aspect of operating system administration. Shell programs, also known as command-line interfaces (CLI), are used to execute commands and interact with the operating system. In this article, we will explore the process of running shell programs in Windows, including the necessary steps, tools, and best practices.
What are Shell Programs?
Shell programs are executable files that contain commands and arguments that are executed by the operating system. They are typically saved with a .bat
or .cmd
extension and are used to automate tasks, manage files, and interact with the operating system.
Why Run Shell Programs in Windows?
Running shell programs in Windows offers several benefits, including:
- Automation: Shell programs can automate repetitive tasks, saving time and effort.
- Flexibility: Shell programs can be used to interact with the operating system in a variety of ways, including executing commands, managing files, and accessing system resources.
- Security: Shell programs can be used to restrict access to sensitive system resources, such as files and directories.
How to Run Shell Programs in Windows
To run a shell program in Windows, you will need to follow these steps:
Step 1: Save the Shell Program
- Save the shell program with a
.bat
or.cmd
extension. - Make sure the file is in a directory that is accessible to the user running the program.
Step 2: Open the Command Prompt
- Open the Command Prompt by searching for «cmd» in the Start menu or by typing
cmd
in the Run dialog box. - Alternatively, you can open the Command Prompt by typing
cmd /k
in the Run dialog box.
Step 3: Execute the Shell Program
- Navigate to the directory where the shell program is saved.
- Type the command to execute the shell program, followed by any arguments or options.
- For example:
myprogram.bat
Step 4: Verify the Execution
- The shell program will execute and display the output.
- You can verify the execution by checking the Command Prompt window or by using the
dir
command.
Tools and Best Practices
- Command Prompt: The Command Prompt is the most commonly used shell program in Windows. It is available in the Start menu or by typing
cmd
in the Run dialog box. - PowerShell: PowerShell is a more powerful shell program that is available in Windows 10 and later versions. It is a more advanced alternative to the Command Prompt.
- Batch Files: Batch files are a type of shell program that are used to automate tasks. They are typically saved with a
.bat
extension and are used to execute commands and interact with the operating system. - Environment Variables: Environment variables are used to store and retrieve values that are used by shell programs. They are typically set using the
set
command.
Common Shell Program Commands
dir
: Displays the contents of a directory.copy
: Copies files and directories.move
: Moves files and directories.ren
: Renames files and directories.echo
: Displays text to the screen.set
: Sets environment variables.cls
: Clears the screen.
Troubleshooting Common Issues
cmd /k
: This command is used to execute a shell program and then return to the Command Prompt.dir /b
: This command is used to display the contents of a directory without showing file names.echo
: This command is used to display text to the screen.set
: This command is used to set environment variables.
Conclusion
Running shell programs in Windows is a fundamental aspect of operating system administration. By following the steps outlined in this article, you can learn how to run shell programs in Windows and automate tasks, manage files, and interact with the operating system. Remember to use the Command Prompt, PowerShell, batch files, and environment variables to run shell programs in Windows.
Additional Resources
- Microsoft Documentation: The official Microsoft documentation for Windows provides detailed information on shell programs and how to use them.
- Windows Subsystem for Linux: The Windows Subsystem for Linux provides a way to run Linux shell programs on Windows.
- Shell Program Examples: There are many examples of shell programs available online that demonstrate how to use them to automate tasks and interact with the operating system.
Code Example
Here is an example of a shell program in batch file format:
@echo off
echo Running shell program
echo %date%
echo %time%
echo %username%
echo %os%
echo %windir%
echo %programfiles%
echo %appdata%
echo %temp%
echo %systemroot%
echo %systemdrive%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %systemroot%
echo %system
Unlock the Future: Watch Our Essential Tech Videos!
- To run shell scripts on Windows, you must enable WSL and Virtual Machine Platform, install a WSL distro, and use “Bash Script.sh” to execute it.
- Alternatively, use third-party tools like Cygwin, Cmder to run Linux commands on top of Windows OS.
If you have worked with Linux distributions and repositories, you would know what shell scripts are. To run shell scripts on top of a Windows computer can be challenging, as the process is not quite straightforward.
Shell scripts are a piece of code written in plain text but have the “.SH” file extension. These are usually used to automate tasks and execute Linux or Unix packages. These are written in Bash and start with “#! /bin/sh
“.
You do not need to install a Linux or Unix distribution alongside your Windows operating system to run a shell script. In this article, we’ll show you how to run a shell script on a Windows PC.
Table of Contents
What are Shell (.SH) Files
The Linux/Unix operating system uses shell or .SH script files to perform certain tasks. In comparison, it can be substituted by Windows batch files for the Windows operating systems, which are used to execute commands on a computer.
The Batch language is a simple and interpreted programming language, unlike C++ or C#. However, you do not have to translate a shell script file to batch to perform similar tasks on a Windows computer, as you do on a Linux PC.
There are different methods to run the same .SH file on a Windows computer as on a Linux system. Let us show you how to run a shell file on a Windows PC.
Run Shell Script (.SH) File on Windows
There are both native and third-party methods to run shell files on a Windows computer. You can use Cygwin, or a third-party tool of your choice, to run a shell script on Windows OS. Alternatively, you can install the Windows Subsystem for Linux feature on Windows and then execute the shell script file through it.
Below you’ll find the methods to run the file using both methods.
Run Shell Script File using Windows Subsystem for Linux (WSL)
This section has been divided into 3 steps to make the process easier to understand. You must do the following 3 things to run a shell script file in the given order:
- Install WSL
- Install a Linux distro
- Run the shell script file
Note: If you are performing these steps on a Virtual Machine, then you will need to enable Nested Virtualization.
Install WSL
Use the following steps to install WSL on your computer:
-
Press the Windows Key + R shortcut keys to open the Run Command box.
-
Type in “optionalfeatures” and press Enter.
Open the Optional Features applet -
Select “Windows Subsystem for Linux” and “Virtual Machine Platform,” and then click Ok.
Enable WSL and Virtual Machine Platform The wizard will now install WSL.
-
When the installation is completed, click “Restart now.”
Restart computer
Once the computer restarts, it is time to install a Linux distro.
Install a Linux Distribution
Use the following steps to install a Linux distribution:
-
When it restarts, press Windows Key + R again to open Run.
-
Type in “cmd” and press CTRL + Shift + Enter to run Command Prompt as administrator.
-
Run the following command to obtain a list of available Linux distros:
wsl --list --online
Get list of all available Linux distros in Command Prompt Note down the name of the distro you want to install.
-
Use the following command to install the preferred distro while substituting its name:
wsl --install -d [DistributionName]
Install a Linux distro in WSL The distro will now begin to download and install.
-
Set up a username and password.
Set up username and password -
Restart the computer.
Now, the next step is to simply run the shell script file.
Run the Shell Script File
Follow the simple steps below to run the shell script file on a Windows PC:
-
Press the Windows Key + R again to open Run.
-
Type in “cmd” and press CTRL + Shift + Enter to run Command Prompt as administrator.
-
Use the CD cmdlet to change the directory to the location of the shell script file.
CD [PathToShellScriptFile]
Change directory to the shell script file location -
Now, use the following command to run the shell script file while substituting [ShellFileName] with the complete and correct name of the shell file:
Bash [ShellFileName].sh
Run the shell script file using Windows Subsystem for Linux
This is how you run a shell script (.SH) file on a Windows computer using WSL.
If this seems like a lengthy process for you, then you can also use the alternative method given below.
Run Shell Script File using Third-Party Tools
You can also run a shell script file on a Windows computer using a third-party tool. In the steps below, we have used Cygwin.
Follow these steps to run a .SH file on a Windows PC:
-
First, download and install Cygwin from their official website.
-
Once installed, run the Cygwin app.
-
In the Cygwin terminal, use the
CD
command to change the directory to the shell script file location.Note: In Cygwin, the syntax to change the directory is different from Command Prompt or PowerShell. Use the following command syntax and example in the image to change your directory in Cygwin.
CD /cygdrive/[driveLetter]/[Subfolder1]/[Subfolder2]
Change directory to the shell script file location in Cygwin -
Now, make the shell script file executable through this command:
chmod +x [ShellFileName].sh
Make the shell script file executable -
Now, use the following syntax to run the shell script file:
./[ShellFileName].sh
Run shell script file on Windows using Cygwin
These are the two methods to run shell script files (.SH) on a Windows PC. Of course, there are many other third-party tools available that allow you to run Linux and Unix files on a Windows computer.
Frequently Asked Questions (FAQs)
How to create a shell script (.SH) file?
I you know what you want to write inside the shell script file, all you need to do is follow these simple rules:
– Create a new notepad file
– Start the script with “#! /bin/sh”
– Write the code below it
– Save the file with a “.sh” extension
– To run the script, type “bash [FileName].sh” after navigating to the folder.
Can I run shell scripts on Windows?
Yes, shell script files can be executed on a Windows computer using Windows Subsystem for Linux, or third-party tools like Cygwin, kiTTY, ConEmu, Cmder, etc.
Why use shell scripts on Windows?
Shell scripts are used to perform repetitive tasks to save time and resources. They are also used to automate tasks. With the WSL feature, Microsoft has opened doors to natively run shell scripts on Windows operating systems.
Quick Links
-
How to Run a Windows Program
-
How to Pass an Argument to a Command
-
How to Run a Built-in Command
-
How to Add Directories to the Path
-
How to Pipe the Output of One Command to Another
In the Creators Update, Windows 10’s Bash shell now allows you to run Windows binaries and standard Command Prompt commands, right from Bash. You can run both Linux and Windows programs from the same Bash shell, or even incorporate Windows commands into a Bash script.
What You Need to Know
Here are some basic details you need to know about this feature:
- User Account: Programs launched from the Bash shell will run as if they were launched by the current Windows user account.
- Permissions: These programs will have the same permissions as the Bash.exe process. So, if you want these commands to have Administrator access, you’ll need to run the Bash shell as Administrator.
-
Working Directory: Windows programs share the same «working directory» as the Bash shell. So, if you run a command that lists the contents of the current directory, it will list the contents of the current working directory in the Bash shell. Use the
cd
command to change working directories.
With that in mind, let’s take a look at how to run a program.
How to Run a Windows Program
To run a Windows program, enter the path to the program’s .exe file in the Bash shell. Remember that your Windows C: drive is available at /mnt/c in Bash. The Bash environment is also case-sensitive, so you have to specify the correct capitalization.
Let’s say you wanted to launch the Ping utility located at C:\Windows\System32\PING.EXE. You’d run the following command:
/mnt/c/Windows/System32/PING.EXE
The following command wouldn’t work, because Bash is case-sensitive:
/mnt/c/windows/system32/ping.exe
This is a bit more complicated if the path contains complex characters like spaces and brackets, like the Program Files folders. You have to «escape» spaces, brackets, and other complex characters by prefixing them with a «\» character.
For example, let’s say you wanted to run the Internet Explorer program located at C:\Program Files (x86)\Internet Explorer\iexplore.exe. You’d have to run the following command in Bash:
/mnt/c/Program\ Files\ \(x86\)/Internet\ Explorer/iexplore.exe
Note the «\» before the space and bracket characters. These characters must be «escaped» or Bash won’t realize the characters are part of a file path.
How to Pass an Argument to a Command
The Bash shell passes arguments directly to the commands you execute.
For example, if you wanted to ping example.com, you’d run:
/mnt/c/Windows/System32/PING.EXE example.com
Or, if you wanted to open the Windows hosts file in Notepad, you’d run:
/mnt/c/Windows/System32/notepad.exe "C:\Windows\System32\drivers\etc\hosts"
You use the standard Windows file path when passing a file path directly to a Windows program. That’s because Bash passes the argument directly. Notepad.exe and other Windows programs expect a Windows file path.
How to Run a Built-in Command
Some Windows commands aren’t .exe files, but are built into the Command Prompt itself. For example, this includes the dir
command you might normally run in a Command Prompt. To run such a command, you need to run the cmd.exe
binary associated with the Command Prompt and pass it the command as an argument with /C, like so:
/mnt/c/Windows/System32/cmd.exe /C command
For example, to run the dir
command built into the Command Prompt, you’d run the following command:
/mnt/c/Windows/System32/cmd.exe /C dir
How to Add Directories to the Path
The Windows Services for Linux environment treats Windows executables similar to the way it treats Linux binaries. This means that you can add a directory containing .exe files to the path and then execute those .exe files directly. For example, to add the System32 directory to your path, you’d run:
export PATH=$PATH:/mnt/c/Windows/System32
You could then run Windows .exe files located in the System32 folder directly, like so:
PING.exe example.com
notepad.exe
cmd.exe /C dir
How to Pipe the Output of One Command to Another
The output of a Windows command can be piped to a Linux command, and vice versa. For example, you can use the ipconfig.exe -all
command to list details about your network interfaces and pipe it to the Linux grep
command to search the output. For example, to list all information about your connection and search for sections matching «IPv4 Address», you’d run:
/mnt/c/Windows/System32/ipconfig.exe -all | grep "IPv4 Address"
That’s the basic process. These commands will also work when incorporated into a Bash script, so you can write a Bash script that incorporates both Windows commands and Linux utilities. If it runs in the Bash shell, it will work in a Bash script.
And, if you want to go the other way, you can use the «bash -c» command to run Bash commands from the standard Windows Command Prompt.
- SS64
- CMD
- How-to
Start a program, command or batch script, opens in a new/separate Command Prompt window.
Syntax START "title" [/D path] [options] "command" [parameters] Key: title Text for the CMD window title bar (required.) path Starting directory. command The command, batch file or executable program to run. parameters The parameters passed to the command. Options: /MIN Start window Minimized. /MAX Start window Maximized. /W or /WAIT Start application and wait for it to terminate. (see below) /LOW Use IDLE priority class. /NORMAL Use NORMAL priority class. /ABOVENORMAL Use ABOVENORMAL priority class. /BELOWNORMAL Use BELOWNORMAL priority class. /HIGH Use HIGH priority class. /REALTIME Use REALTIME priority class.
/B Start application without creating a new window. In this case Ctrl-C will be ignored - leaving Ctrl-Break as the only way to interrupt the application. /I Ignore any changes to the current environment, typically made with SET. Use the original environment passed to cmd.exe /NODE The preferred Non-Uniform Memory Architecture (NUMA) node as a decimal integer. /AFFINITY The processor affinity mask as a hexadecimal number. The process will be restricted to running on these processors. Options for running 16-bit Windows programs, on Windows 10 only: /SEPARATE Start in separate memory space. (more robust) 32 bit only. /SHARED Start in shared memory space. (default) 32 bit only.
Always include a TITLE this can be a simple string like «My Script» or just a pair of empty quotes «»
According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.
If command is an internal cmd command or a batch file then the command processor CMD.exe is run with the /K switch. This means that the window will remain after the command has been run.
In a batch script, a START command without /wait will run the program and just continue, so a script containing nothing but a START command will close the CMD console and leave the new program running.
Document files can be invoked through their file association just by typing the name of the file as a command.
e.g. START «» MarchReport.docx will launch the application associated with the .docx file extension and load the document.
To minimise any chance of the wrong exectuable being run, specify the full path to command or at a minimum include the file extension: START «» notepad.exe
If you START an application without a file extension (for example WinWord instead of WinWord.exe)then the PATHEXT environment variable will be read to determine
which file extensions to search for and in what order.
The default value for the PATHEXT variable is: .COM;.EXE;.BAT;.CMD
Start — run in parallel
The default behaviour of START is to instantiate a new process that runs in parallel with the main process. For arcane technical reasons, this does not work for some types of executable, in those cases the process will act as a blocker, pausing the main script until it’s complete.
In practice you just need to test it and see how it behaves.
Often you can work around this issue by creating a one line batch script (runme.cmd ) to launch the executable, and then call that script with START runme.cmd
Start /Wait
The /WAIT option should reverse the default ‘run in parallel’ behaviour of START but again your results will vary depending on the item being started, for example:
Echo Starting START /wait "job1" calc.exe Echo DoneThe above will start the calculator and wait before continuing. However if you replace calc.exe with Winword.exe, to run Word instead, then the /wait will stop working, this is because Winword.exe is a stub which launches the main Word application and then exits.
A similar problem will occur when starting a batch file, by default START will run the equivalent of CMD /K which opens a second command window and leaves it open. In most cases you will want the batch script to complete and then just close its CMD console to resume the initial batch script. This can be done by explicitly running CMD /C …
Echo Starting START /wait "demojob" CMD /c demoscript.cmd Echo DoneAdd /B to have everything run in a single window.
In a batch file, an alternative is to use TIMEOUT to delay processing of individual commands.
START vs CALL
Starting a new process with CALL, is very similar to running START /wait, in both cases the calling script will (usually) pause until the second script has completed.
Starting a new process with CALL, will run in the same shell environment as the calling script. For a GUI application this makes no difference, but a second ‘called’ batch file will be able to change variables and pass those changes back to the caller.
In comparison START will instantiate a new CMD.exe shell for the called batch. This will inherit variables from the calling shell, but any variable changes will be discarded when the second script ends.
Run a program
To start a new program (not a batch script), you don’t have to use CALL or START, just enter the path/file to be executed, either on the command line or within a batch script. This will behave as follows:
- On the command line, CMD.EXE does not wait for the application to terminate and control immediately returns to the command prompt.
- Running a program from within a batch script, CMD.EXE will pause the initial script and wait for the application to terminate before continuing.
- If you run one batch script from another without using either CALL or START, then the first script is terminated and the second one takes over.
Search order:
- Running a program from CMD will search first in the current directory and then in the PATH.
- Running a program from PowerShell will search first in the PATH and then in the current directory.
- The Windows Run Line (win+r) will search first in App Paths [defined in HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths] and then the PATH
Multiprocessor systems
Processor affinity is assigned as a hex number but calculated from the binary positions (similar to NODRIVES)
Hex Binary Processors
1 00000001 Proc 1
3 00000011 Proc 1+2
7 00000111 Proc 1+2+3
C 00001100 Proc 3+4 etcSpecifying /NODE allows processes to be created in a way that leverages memory locality on NUMA systems. For example, two processes that communicate with each other heavily through shared memory can be created to share the same preferred NUMA node in order to minimize memory latencies. They allocate memory from the same NUMA node when possible, and they are free to run on processors outside the specified node.
start /NODE 1 app1.exe
start /NODE 1 app2.exeThese two processes can be further constrained to run on specific processors within the same NUMA node.
In the following example, app1 runs on the low-order two processors of the node, while app2 runs on the next two processors of the node. This example assumes the specified node has at least four logical processors. Note that the node number can be changed to any valid node number for that computer without having to change the affinity mask.
start /NODE 1 /AFFINITY 0x3 app1.exe
start /NODE 1 /AFFINITY 0xc app2.exe
Running executable (.EXE) files
When a file that contains a .exe header, is invoked from a CMD prompt or batch file (with or without START), it will be opened as an executable file. The filename extension does not have to be .EXE. The file header of executable files start with the ‘magic sequence’ of ASCII characters ‘MZ’ (0x4D, 0x5A) The ‘MZ’ being the initials of Mark Zibowski, a Microsoft employee at the time the file format was designed.
Command Extensions
If Command Extensions are enabled, external command invocation through the command line or the START command changes as follows:
Non-executable files can be invoked through their file association just by typing the name of the file as a command. (e.g. example.docx would launch the application associated with the .docx file extension). This is based on the setting in HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ext\OpenWithList, or if that is not specified, then the file associations — see ASSOC and FTYPE.
When executing a command line whose first token is the string CMD without an extension or path qualifier, then CMD is replaced with the value of the COMSPEC variable. This prevents picking up CMD.EXE from the current directory.
When executing a command line whose first token does NOT contain an extension, then CMD.EXE uses the value of the COMSPEC environment variable. This prevents picking up CMD.EXE from the current directory.
When executing a command line whose first token does NOT contain an extension, then CMD.EXE uses the value of the PATHEXT environment variable to determine which extensions to look for and in what order. The default value for the PATHEXT variable is: .COM;.EXE;.BAT;.CMD Notice the syntax is the same as the PATH variable, with semicolons separating the different elements.
When searching for an executable, if there is no match on any extension, then looks to see if the name matches a directory name. If it does, the START command launches the Explorer on that path. If done from the command line, it is the equivalent to doing a CD /D to that path.
Errorlevels
If the command is successfully started ERRORLEVEL =unchanged, typically this will be 0 but if a previous command set an errorlevel, that will be preserved (this is a bug).
If the command fails to start then ERRORLEVEL = 9059
START /WAIT batch_file — will return the ERRORLEVEL specified by EXIT
START is an internal command.
Examples
Start a program in the current directory:
START «Demo Title» example.exe
Start a program giving a fulll path:
START «Demo 2» /D «C:\Program Files\ACME Corp\» «example.exe»
Alternatively:
START «Demo 2» «C:\Program Files\ACME Corp\example.exe»
or:
CD /d «C:\Program Files\ACME Corp\»
START «Demo 2» «example.exe»
Start a program and wait for it to complete before continuing:
START «Demo 3» /wait autocad.exe
Open a file with a particular program:
START «Demo 4» «C:\Program Files\Microsoft Office\Winword.exe» «D:\Docs\demo.txt»
Run a minimised Login script:
CMD.exe /C START «Login Script» /Min CMD.exe /C Login.cmd
In this example the first CMD session will terminate almost immediately and the second will run minimised.
The first CMD is required because START is a CMD internal command. An alternative to this is using a shortcut set to open minimised.
Open Windows Explorer and list the files in the current folder (.) :
C:\any\old\directory> START .
Open a webpage in the default browser, note the protocol is required (https://):
START
https://ss64.com
Open a webpage in Microsoft Edge:
%windir%\explorer.exe microsoft-edge:https://ss64.com
or with a hard-coded path:
«C:\Program Files (x86)\Microsoft Edge\Application\msedge.exe»https://ss64.com"%windir%\explorer.exe shell:Appsfolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge" https://ss64.com
Connect to a new printer: (this will setup the print connection/driver):
START \\print_server\printer_name
Start an application and specify where files will be saved (Working Directory):
START /D C:\Documents\ /MAX «Maximised Notes» notepad.exe
“Do not run; scorn running with thy heels” ~ Shakespeare, The Merchant of Venice
Related commands
WMIC process call create «c:\some.exe»,»c:\exec_dir» — This method returns the PID of the started process.
CALL — Call one batch program from another.
CMD — can be used to call a subsequent batch and ALWAYS return even if errors occur.
TIMEOUT — Delay processing of a batch file/command.
TITLE — Change the title displayed above the CMD window.
RUN commands Start ➞ Run commands.
How-to: Run a script — How to create and run a batch file.
How-to: Autoexec — Run commands at startup.
ScriptRunner — Run one or more scripts in sequence.
Q162059 — Opening Office documents.
Equivalent PowerShell: Start-Process — Start one or more processes.
Equivalent bash command (Linux) : open — Open a file in it’s default application.
Equivalent macOS command: open — Open a file in a chosen application.
Copyright © 1999-2025 SS64.com
Some rights reserved
Are you interested in working with shell scripts on Windows? Thanks to a recent addition by Microsoft, you can now use Windows Subsystem for Linux to make this happen.
Once you enable shell scripts in Windows 10, you can start creating shell scripts of your own. Shell scripts are great for automating simple tasks. You can also work on open-source Linux-based projects if that’s an area of interest to you. Finally, you’ll learn how to execute shell scripts on Windows 10. Continue reading to learn more.
What is Linux?
Linux is an open-source operating system that is highly popular among computer enthusiasts. Linux competes with other computer operating systems like Microsoft’s Windows, Apple’s macOS, and mobile operating systems like Android and iOS.
The Linux operating system was originally developed by Linus Torvalds back in 1991. The Linux kernel was designed as a Unix-like operating system. Unix was an early operating system created by Bell Labs in 1969. Today, modern Linux distributions are still Unix-like, meaning they retain the basic structure and properties that Unix had. An example of a non-Unix operating system would be Microsoft Windows.
The top Linux distributions have changed over the years, but as of 2022, Ubuntu, Debian, CentOS, Fedora, and Red Hat rank as the top 5 most popular options.
What is Bash?
When Linus Torvalds created Linux, he included a Unix shell called Bash. Bash had been created just two years before, in 1989 by Brian Fox. Bash has been the longtime default for Linux and was also the default for Apple macOS until it was replaced by Z shell in 2019.
Until 2016, Windows users could not use the Linux kernel or Bash at all. Windows first introduced the Windows Subsystem for Linux (WSL) beta with Windows 10 version 1607 update. About a year later, in October 2017, WSL was fully released in Windows 10 version 1709. Microsoft developed WSL for hobbyists and developers who want to work on open-source Linux-based projects.
It’s important to note that WSL is not preinstalled on Windows 10. If you would like access to create and run shell scripts on Windows 10, you will need to manually install WSL or join the Windows insider program.
What is a shell script?
A Shell script is a type of script that cannot be run without a Unix shell. Further, a shell script is a series of commands that are executed line by line by the command line.
You can use shell scripts to automate processes and avoid repetitive tasks. Instead of manually completing each step in a series, you can execute a script, and the command line will handle the rest.
For example, if you find yourself regularly stopping processes that are hogging your CPU, you can automate this process with a script. When you execute the script, it may be designed to find a set of processes using CPU resources and request to kill them.
Enabling shell scripts in Windows 10
- Click on the Start (Windows) button and enter “Control Panel” into the search bar. Click Open on the Control Panel result on the right-hand side.
- Within the Control Panel window, find and click on Programs.
- Now, from the Programs window, find Click Turn Windows features on or off underneath the Programs and Features header.
- In the Windows Features window, scroll to the very bottom of the window. Check the Windows Subsystem for Linux option. Then click OK.
- Windows will automatically install the necessary files. When the installation is complete, select Restart Now.
- When your computer restarts, you need to install Ubuntu from the Microsoft store.
- After installation, make sure you open Ubuntu and see it up. You are now ready to use scripts on your Windows 10 machine.
If you encounter any issues with Ubuntu or bash commands not working correctly, you may want to check that Virtualization is turned on in your BIOS. The most updated WSL version, WSL 2, runs the Linux kernel using virtualization technology. This means a virtual machine needs to be able to run on your system.
Now that Windows Subsystem for Linux and Ubuntu has been installed, you are ready to start creating shell scripts in Windows 10. You may be tempted to write bash scripts with Notepad, but this is not recommended. Because Notepad is designed for Windows/DOS systems, the line endings will differ from those that are found at the end of Unix/Linux line endings.
Text editors for shell scripts
You should use software that is designed to convert to Unix/OSX end-of-line characters. The best open-source software available for this is Notepad++. Amazingly, Notepad++ is lovingly maintained and developed by a single individual, Don Ho.
If you try Notepad++ and don’t like it, you can try another fan favorite, nano. Nano is a text editor for Unix/Linux systems. You can easily create shell scripts that will run in bash, using nano. Download nano to get started.
Example shell scripts
Let’s look at some basic shell scripts, so you can learn more about what you are going to be coding and see how some formatting and syntax work.
1. Hello World!
echo "Hello World!"
This script will print out the infamous Hello World! Notice that echo can be used as a print command when not combined with any other modifiers. It will print the string on a new line. If you add the -n modifier, the output will print on the same line.
2. Sum two numbers
If you want to do some basic arithmetic, you might have a script that looks like:
# Add two numbers together
((sum=25+35))
# Print the sum of the numbers
echo $sum
Note that the # symbol is used to make comments that are not expressed. The output of this script will print the sum of 25+35, which is 60.
3. Take user input
The following script will ask for the user’s name and then use the read command to take the user’s input. Then the user’s name is passed into the following expression, ultimately welcoming you to Windows Subsystem for Linux.
echo "What is your name?"
read name
echo "Welcome $name to Windows Subsystem for Linux."
Write basic shell scripts in Windows 10
Continue reading to learn how to write basic shell scripts in Windows 10 using Notepad++.
- Click the Start button and search for “Notepad++” and click Run as administrator on the right-hand side.
- Now you can create your script.
- Once your script is complete, you need to use the EOL Conversion option available in Notepad++. Click Edit and locate EOL Conversion from the dropdown menu. Hover over this option and then select UNIX/OSX Format from the next dropdown menu.
- Now select File and then Save As. Make sure to name your file something you will recognize and add .sh to make it a shell script file.
- Once the shell script is saved, continue to the next section to learn how to run your own shell scripts.
How to run shell scripts (.sh files) on Windows 10
You’ve created your first shell scripts, and it’s time to execute the sh file. Remember that when using WSL, you can only use Linux commands and utilities. Windows 10 programs will not work in bash scripts. To execute a script file, follow these step-by-step instructions:
- Click on the Start (Windows) button and enter “Command Prompt” into the search bar. Click Run as administrator on the Command Prompt result on the right-hand side.
- Navigate to the folder where the script file is saved. You move around in the command prompt using the cd command. For example, if you want to access the Documents folder, you would enter the following and press Enter:
cd C:\Users\Username\OneDrive\Documents
Note: Username would be the username that you set up for yourself when you registered your computer.
- Now enter bash file-name.sh, where file-name is the whatever you’ve named your script file.
bash file-name.sh
The script will execute, and if there are any outputs or print statements included in the script, the output will be returned.
Bash scripts running on Windows 10
You’ve made it far and learned a ton of information in one go. Command-line utilities, different operating systems, and learning to write and execute shell scripts can be difficult topics. In fact, these topics will take time to master. You have a ton of learning to do for scripting, but resources are available to help you all over the internet.
Within this guide, you learned the basics of Linux and Bash. You learned what shell scripts are and that you need to specifically enable Windows Subsystem for Linux (WSL) to use them. You learned how to create shell scripts using Notepad++ and how to execute the scripts in bash. Enjoy experimenting!