Skip to main content

How to Clear the Command Prompt Screen in Batch Script

When you run a script that produces a lot of output, the command prompt window can become cluttered with old information. To improve readability and create a clean, professional-looking interface for your script, you often need to clear the screen of all previous text. This is especially useful for scripts that display a menu, show a refreshing status screen, or simply want to start with a clean slate.

This guide will teach you how to use the simple and universally known CLS command to clear the screen, explain its behavior, and show you a practical example of how it's used to create a simple menu system.

The Core Command: CLS (Clear Screen)

The CLS command is one of the most fundamental commands in the Windows command-line environment. Its purpose is singular and simple: it Clears the Screen.

Syntax: CLS

The command takes no arguments or switches.

Basic Example: A Simple Screen Clear

This script demonstrates the most basic use of the CLS command.

@ECHO OFF
ECHO This is the first set of information.
ECHO It contains multiple lines of text that might be confusing.
ECHO We are about to clear the screen.
ECHO.
PAUSE

REM --- This is the clear screen command ---
CLS

ECHO This is the second set of information.
ECHO Notice that everything from before is now gone.
ECHO The screen is clean and easy to read.
ECHO.
PAUSE

Let's see an example of Console Interaction. Before the CLS command, the screen will look like this:

This is the first set of information.
It contains multiple lines of text that might be confusing.
We are about to clear the screen.

Press any key to continue . . .

After the user presses a key, the CLS command runs, and the screen is instantly replaced with this:

This is the second set of information.
Notice that everything from before is now gone.
The screen is clean and easy to read.

Press any key to continue . . .

How CLS Works

The CLS command clears the viewport of the command prompt window. This means it removes all text currently visible on the screen and moves the cursor to the top-left position (row 1, column 1).

It is important to understand what it does not do:

  • It does not clear the scrollback buffer. If you scroll up in the command prompt window, you will still be able to see the text that was on the screen before the CLS command was run.
  • It does not reset the environment. All variables, the current directory, and other settings are completely unaffected.
  • It does not affect the window's title or colors.

Common Pitfalls and How to Solve Them

The CLS command is so simple that it has very few pitfalls. The main consideration is one of user experience.

  • Clearing Too Much: Be careful not to use CLS at a point where the user might still need to read or copy the text that's on the screen. A CLS command is an irreversible action on the current viewport.

    • Solution: It's a good practice to place a PAUSE command before a CLS if you think the user might need to see the information you're about to clear.
  • Using in Unattended Scripts: In a script that is logging its output to a file (e.g., MyScript.bat > log.txt), the CLS command will insert special control characters into the log file. When you open the log file in a text editor, you might see a strange character (like a form feed ) where the CLS was executed.

    • Solution: This is generally harmless, but if you want to avoid it, you can check if the script is running interactively. However, for most logging, this minor issue can be safely ignored.

Practical Example: Creating a Refreshing Menu Screen

This is the most common and effective use case for CLS. The script displays a menu of options. After the user makes a choice and the action is completed, the script clears the screen and redisplays the menu, creating a clean and continuous user experience.

@ECHO OFF
:Menu
REM --- Start by clearing the screen for a fresh display ---
CLS

ECHO ============================
ECHO MAIN MENU
ECHO ============================
ECHO.
ECHO 1. Check System Status
ECHO 2. Run Backup
ECHO 3. Exit
ECHO.

CHOICE /C 123 /N /M "Please select an option: "

IF ERRORLEVEL 3 GOTO :EOF
IF ERRORLEVEL 2 GOTO :RunBackup
IF ERRORLEVEL 1 GOTO :CheckStatus

:CheckStatus
ECHO --- Checking system status... ---
TIMEOUT /T 2 > NUL
PAUSE
GOTO :Menu

:RunBackup
ECHO --- Running backup... ---
TIMEOUT /T 3 > NUL
PAUSE
GOTO :Menu

By placing CLS at the very beginning of the :Menu loop, we ensure that every time the menu is shown, it's on a clean screen, free from the output of the previous action.

Conclusion

The CLS command is a simple but essential tool for controlling the visual presentation of your batch script's output.

  • Its only function is to clear the command prompt's screen.
  • It takes no arguments.
  • It is most effective when used to reduce clutter before displaying a new screen of information, such as in a menu system or a refreshing status display.

By using CLS thoughtfully, you can make your scripts significantly cleaner, more professional, and easier for users to read and interact with.