5 Ways to Create a Batch (.bat) File on Windows 11

All you need is a Notepad app on your PC

Reading time icon 6 min. read


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

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

Key notes

  • You can use Command Prompt or PowerShell to run a batch file to automate regular tasks.
  • To know the steps to create a batch file on Windows 11, keep reading!
create a batch file Windows 11

Performing repetitive tasks or running a series of commands might be essential to your computing routine, but it can take a lot of time. That’s where creating a Batch (.bat) file on Windows 11 comes to the rescue.

In this guide, we will discuss step-by-step instructions to create a batch script file on Windows 11 to automate tasks while reducing errors and saving time.

Batch files can be beneficial for novice and experienced users:

  • Automate repetitive tasks and streamline workflows, saving significant time & effort.
  • Consistent execution of tasks, reducing human error and improving efficiency.
  • Carry out specific tedious tasks like backups, system configuration, software installation & more.
  • Help you execute commands on multiple computers at the same time.
  • Can carry out complex procedures that involve multiple steps.
  • Are portable and can be shared with other computers.

How do I create a batch file in Windows 11?

1. Create a basic batch file

  1. Press the Windows key, type notepad, and click Open.Open Notepad create a batch file Windows 11
  2. Type the following lines:
    • @ECHO OFF
      ECHO Hi, this is my first batch file.
      PAUSE
  3. Here @ECHO OFF – Disables the display prompts & shows content; ECHO – Prints the text after the space; PAUSE – Keeps the window open after executing commands.
  4. Go to File and click Save as.File - Save as
  5. Name the file Test.bat, and for Save as type, select All files, then click Save.Notepad_Save file
  6. Once created, locate the file and double-click to open it. On the Command Prompt window, you will see the Hi, this is my first batch file message.WindowsTerminal_ run the script

You can also save the .reg files in .bat file format to automate the modification of your registry files, which can minimize the risks involved.

2. Access network drives and folders

  1. Press the Windows key, type notepad, and click Open.Open Notepad create a batch file Windows 11
  2. Type the following line after replacing the driver letter F with the one you want: Echo Create new F: drive mapping
  3. Copy & paste the following command after replacing the Network path with the path you want to map to: @net use F: \Network path /persistent: yes
  4. Repeat the process if you want to add multiple drives. Type the following lines:
    • : exit
      @pause
      Notepad_crreate a batch file on Windows 11
  5. Click File, then choose Save As. Now name the file and add a .bat extension. For Save as type, select All files, then click Save.Notepad_Save file
  6. Once the file is created, locate and double-click it to execute the task.

3. Add user inputs

To create a batch file that takes user input and displays a customized message, follow these steps:

  1. Press the Windows key, type notepad, and click Open.Open Notepad create a batch file Windows 11
  2. Type the following script:
    • @echo off
      : start
      set /p input = Enter the Name:
      echo %input% We are thrilled to welcome you to the event!
      pause
      go to start
      Notepad_user inpiut commands  create batch file Windows11
  3. You can change the Enter the Name instruction and the personalized message you see after that as per your task & preferences.
  4. Click File, then Save As. Name the file, add a .bat file extension and for Save as type, select All files, then click Save.Notepad_Save file
  5. Once the file is created, locate and double-click it to open it in the Command Prompt & execute the task.

4. Automate repetitive tasks

  1. Press the Windows key, type notepad, and click Open.Open Notepad create a batch file Windows 11
  2. Here, we will show you how we automated the process of opening frequently used apps on our computer.
  3. This is the script we used to open Google Chrome, Word, Slack, and ShareX:
    • @echo off
      cd "C:\Program Files\Google\Chrome\Application\chrome.exe"
      start chrome.exe
      start โ€“ "C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE"
      cd "C:\Program Files\ShareX\ShareX.exe"
      start ShareX.exe
      Exit
      Notepad_create a batch file Windows 11
  4. You can use the script, but replace the app path with the one you want to open. Click the File menu and choose Save As.
  5. Name the file, add a .bat extension, for Save as type, select All files, then click Save.

5. Edit lines of code with the command window

  1. Press the Windows key, type notepad, and click Open.
  2. This script can help you replace lines in a simple code; let’s assume you have a file named code.txt, which has oldtext mentioned multiple times, and you want to replace it with newtext.Notepad_edit commands create a batch file Windows 11
  3. For that, you can use this script:
@echo off

setlocal enabledelayedexpansion

set "inputFile=code.txt"
set "outputFile=code_modified.txt"
set "oldText=oldtext"
set "newText=newtext"

if not exist "%inputFile%" (
    echo Input file not found.
    exit /b
)

(
    for /f "usebackq delims=" %%a in ("%inputFile%") do (
        set "line=%%a"
        set "line=!line:%oldText%=%newText%!"
        echo !line!
    )
) > "%outputFile%"

echo Lines with '%oldText%' replaced by '%newText%' have been written to '%outputFile%'.

endlocal

You need to change code.txt with your file name and replace oldtext and newtext with the lines you want to use.

Now save the file using these steps:

  1. Click File and choose Save As. Name the file, add a .bat extension, for Save as type, select All files, then click Save.Notepad_Save file
  2. Locate and double-click the file to open it in the Command Prompt window & execute the task.

Now that you have created .bat file, you can use the Windows Task Scheduler to schedule it to run automatically at specific intervals.

How do I edit a batch file in Windows 11?

To edit a batch file, you can open Notepad and click File>Open, or you can locate and right-click the file and choose Edit from the context menu.

You can make the required changes once the file is opened in Notepad. Once edited, click Ctrl + S to save the changes.

To run the batch file in Windows 11, you can locate it in the File Explorer window and double-click it or open Command Prompt and use the cd command to go to the folder where the batch file is located, then type the batch file name and press Enter.

You should always save the batch script file to an accessible location like your user account’s Document or Desktop folder.

Remember that the batch files can execute commands, interact with your system, automate tasks, and edit simple codes; however, if you want to do sophisticated editing or code manipulation, we suggest you use a programming language like Python or PowerShell.

What tasks would you carry on using batch files? Feel free to mention them in the comments section below.

More about the topics: Windows 11

User forum

0 messages