How to Play a Sound in a Batch Script
Providing audible feedback can make a batch script more user-friendly. A simple "beep" or a "chime" can signal the successful completion of a long task, alert a user to an error, or notify them that their input is required. While cmd.exe has no native PLAY command, there are several simple and effective ways to produce sounds using built-in Windows capabilities.
This guide will teach you the simplest method of playing a sound by echoing a special "bell" character. It will also cover the more powerful and flexible modern approach of using a PowerShell one-liner to play any .wav file on the system.
Method 1: The Simple Beep (The Bell Character)
This is the oldest, simplest, and most universally compatible method. It relies on the ASCII Bell Character (character code 7). When this special, non-printable character is sent to the command prompt's output stream, the console window emits a simple "beep" sound.
The challenge is that you cannot simply type this character. The easiest way to generate it is by using ECHO with a special syntax.
Command: ECHO ^G
To type this, you do not type a caret and a G. Instead, you type ECHO followed by a space, and then you press Ctrl+G. In the command prompt (and most text editors like Notepad++), this will appear as ^G.
When this command is executed, you will hear the default system beep.
For example:
@ECHO OFF
ECHO About to beep...
TIMEOUT /T 2 > NUL
REM The line below was created by typing Ctrl+G
ECHO ^G
ECHO Beep complete.
This is a quick and easy way to get a simple audio alert.
Method 2 (Recommended): Playing Any .wav File with PowerShell
The simple beep is very limited. If you want to play a more pleasant or specific sound, you need a way to play an audio file. The best built-in tool for this is PowerShell. It has a System.Media.SoundPlayer class that can play any .wav file.
This method allows you to use the standard Windows system sounds, which are located in C:\Windows\Media.
Syntax: powershell -Command "(New-Object Media.SoundPlayer 'path\to\sound.wav').PlaySync()"
New-Object Media.SoundPlayer: Creates a new sound player object.'path\to\sound.wav': The full path to the.wavfile you want to play..PlaySync(): This method plays the sound and waits for it to finish before the script continues. You can use.Play()to play the sound in the background and let your script continue immediately.
Basic Example: Alerting the User
Let's use PowerShell to play the standard Windows "exclamation" sound.
@ECHO OFF
SET "SoundFile=C:\Windows\Media\Windows Exclamation.wav"
ECHO --- Playing a System Sound ---
ECHO.
ECHO This script will now play the Windows Exclamation sound.
REM Check if the sound file exists before trying to play it.
IF EXIST "%SoundFile%" (
powershell -Command "(New-Object Media.SoundPlayer '%SoundFile%').PlaySync()"
) ELSE (
ECHO Sound file not found. Playing a simple beep instead.
ECHO ^G
)
ECHO.
ECHO --- Sound playback complete ---
This script is robust: if it can find the specific .wav file, it plays it; otherwise, it falls back to the simple beep.
Common Pitfalls and How to Solve Them
-
Sound Card / Volume Issues: The most obvious reason for failure is that the computer's sound is muted, the volume is too low, or there is no audio device. Your script cannot control this, but it's the first thing to check if you don't hear anything.
-
Playing Other File Types (
.mp3, etc.): TheSystem.Media.SoundPlayerclass can only play.wavfiles. It cannot play.mp3,.wma, or other modern audio formats.- Solution: For other formats, you would need to use a more advanced PowerShell class (like
WindowsMediaPlayer) or launch an external command-line audio player. For simple script notifications, sticking to.wavfiles is the easiest path.
- Solution: For other formats, you would need to use a more advanced PowerShell class (like
-
Path with Spaces: When using the PowerShell method, the path to the
.wavfile might contain spaces.- Solution: The PowerShell command shown (
'... 'path' ...') uses single quotes around the path. This correctly handles spaces and is the recommended syntax.
- Solution: The PowerShell command shown (
Practical Example: A "Task Complete" Notification
This script simulates a long-running process (like a backup or file conversion) and plays a notification sound to alert the user that the task has finished.
@ECHO OFF
SETLOCAL
REM --- Use the Windows "Notify" sound for a pleasant alert ---
SET "CompletionSound=C:\Windows\Media\Windows Notify System Generic.wav"
ECHO --- Starting Long Process ---
ECHO This will take 10 seconds. You will be notified upon completion.
ECHO.
REM --- Simulate a long task ---
TIMEOUT /T 10 /NOBREAK > NUL
ECHO.
ECHO --- Process Complete ---
ECHO Playing notification sound...
IF EXIST "%CompletionSound%" (
REM Play the sound in the background so the script can end.
powershell -Command "(New-Object Media.SoundPlayer '%CompletionSound%').Play()"
) ELSE (
REM Fallback for 3 beeps if the sound is missing.
ECHO ^G
TIMEOUT /T 1 /NOBREAK > NUL
ECHO ^G
TIMEOUT /T 1 /NOBREAK > NUL
ECHO ^G
)
ENDLOCAL
Conclusion
Adding audible alerts to your batch scripts is a simple way to make them more interactive and user-friendly.
- For a quick, simple, and universally compatible "beep", use the
ECHO ^G(typed asCtrl+G) method. - For a more flexible and professional result that can play any
.wavfile, the PowerShell one-liner is the highly recommended modern method.powershell -Command "(New-Object Media.SoundPlayer 'sound.wav').PlaySync()"
By leveraging PowerShell, you can easily integrate any of the standard Windows system sounds into your scripts to provide clear and effective notifications.