How to Turn the Monitor Off in a Batch Script
Turning off the monitor from a script is a useful command for power management, security, or simple convenience. You might want to create a shortcut on your desktop to instantly turn off the displays, or have a script that turns off the monitor after a long backup job is complete.
While cmd.exe has no built-in command for this, the task is easily accomplished. The classic and most common method is to use a PowerShell one-liner, as it provides a direct command to control the monitor's power state. A lesser-known but equally effective method is to use a VBScript helper. This guide will cover both robust, built-in methods.
The Challenge: No Native Monitor Off Command
The batch command processor has no direct control over hardware states like monitor power. It cannot simply run a command like MONITOR /OFF. This is a low-level system function that requires interacting with the Windows GUI or power management APIs. To do this from a script, we must call a more powerful, built-in scripting engine that can access these functions.
Method 1 (Recommended): Using a PowerShell One-Liner
This is the modern, direct, and most recommended method. It uses PowerShell to send a specific "SendMessage" command to the operating system that mimics the power-off signal.
Syntax:
powershell -Command "(Add-Type '[DllImport(\"user32.dll\")]public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2)"
This is a long and complex-looking command, but it is a self-contained, copy-and-paste solution.
@ECHO OFF
ECHO Turning the monitor(s) off in 3 seconds...
TIMEOUT /T 3 > NUL
powershell -Command "(Add-Type '[DllImport(\"user32.dll\")]public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2)"
When this command runs, all connected monitors will immediately go into standby/power-save mode. They will wake up again with any user activity (moving the mouse, pressing a key).
The Core PowerShell Logic Explained
Let's break down the PowerShell command:
Add-Type ...: This part dynamically creates a reference to a function from a core Windows library.[DllImport(\"user32.dll\")]: It specifies that we want to use a function fromuser32.dll, which manages the Windows user interface.SendMessage: This is the specific Win32 API function we are calling. It is used to send messages between applications and the OS.
::SendMessage(-1,0x0112,0xF170,2): This is the actual call to the function with four special parameters.-1:hWnd(Window Handle). A value of-1means "broadcast this message to all windows."0x0112:hMsg(Message). This is the code forWM_SYSCOMMAND.0xF170:wParam(Parameter 1). This is the code forSC_MONITORPOWER.2:lParam(Parameter 2). This is the specific power state:2means "turn off." (-1would be on,1would be low-power).
Method 2 (Alternative): Using a Temporary VBScript
This method achieves the same result and is useful for older systems where PowerShell might not be available or preferred. It works on the same principle: the batch script writes a .vbs file that calls the same SendMessage API.
@ECHO OFF
SET "VBS_FILE=%TEMP%\monitor_off.vbs"
ECHO --- Creating temporary VBScript to turn off monitor ---
(
ECHO Set oShell = CreateObject("WScript.Shell")
ECHO oShell.Run "powershell -Command ""(Add-Type '[DllImport(\""user32.dll\"")]public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2)"""
) > "%VBS_FILE%"
REM Using wscript.exe runs the script with no console window.
wscript.exe "%VBS_FILE%"
DEL "%VBS_FILE%"
This VBScript itself is just a wrapper for the same PowerShell command, which is the most direct way. Older VBS-only solutions are much more complex.
Common Pitfalls and How to Solve Them
-
Administrator Rights: This is a user-level action. It does not require administrator rights to turn off the monitor for the current user's session.
-
Monitor Wakes Up Immediately: If your script turns the monitor off and then immediately exits, the command prompt window closing can be registered as "user activity" and may instantly wake the monitor back up.
- Solution: Add a short
PAUSEorTIMEOUTafter the command if you are running it interactively. For a script that should exit silently, this is less of an issue.
- Solution: Add a short
Practical Example: A "Lock and Turn Off Monitor" Script
This is an extremely useful utility script. It combines the command to lock the workstation with the command to turn off the monitor, creating a single action that both secures the PC and saves power.
@ECHO OFF
TITLE Secure and Sleep
ECHO --- Locking Workstation and Turning Off Monitor ---
ECHO.
ECHO This window will close automatically.
ECHO.
REM --- Step 1: Lock the workstation ---
RUNDLL32.EXE user32.dll,LockWorkStation
REM --- Step 2: Turn off the monitor ---
REM There will be a slight delay before the monitor turns off.
TIMEOUT /T 1 /NOBREAK > NUL
powershell -Command "(Add-Type '[DllImport(\"user32.dll\")]public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2)"
You can save this as LockAndSleep.bat and create a desktop shortcut to it for a convenient, one-click security tool.
Conclusion
While batch scripting has no native command to control monitor power, it can easily delegate this task to the powerful, built-in PowerShell engine.
- The PowerShell
SendMessageone-liner is the modern, direct, and highly recommended method. - The command is a single, self-contained line that can be copied and pasted into any script.
- This operation does not require administrator rights.
- This technique can be combined with other commands like
RUNDLL32 ... LockWorkStationto create powerful utility scripts.