Paul Hempshall

Website Cyber Security Professional

How to play a sound automatically with VBScript, PowerShell and Windows Task Scheduler

Published: September 19, 2022 2:35 pm

Guide

This guide will show you how to schedule playing an audio file in the background without showing the application window using PowerShell and the Windows Task Scheduler.

The example here will use the stock market bells on the opening and closing times of the market. Other uses could be to regulate work or study patterns, using different sounds for your needs.

The scheduled task will flow like this. The action will trigger at specific times and run a VBScript invoking a background shell to launch our PowerShell script. This PowerShell script plays our audio file.

The Scripts

First we need to create our scripts. We will have two scripts. One to play the audio and the other to launch it in a background shell. You can download the complete scripts and sound file used in this example.

PowerShell

The PowerShell script is used to play the audio. Running this file on it's own it launches a console window, plays the audio, and exits.

Create a file, I named mine stock-market.bat, and put in the code below. Change the red highlighted text to the location of your audio file.

powershell -c (New-Object Media.SoundPlayer "C:\Windows\Media\stock-bells.wav").PlaySync();
Running the file standalone will display the command prompt for the duration of the audio.

VBScript

Next we create our file for the VBScript. I named mine stock-market.vbs. This script will create a new shell, run a command (the above file we created), and tell the shell to remain in the background. Change the red highlighted text to the location of the PowerShell file you created above.

Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run chr(34) & "C:\Windows\Media\stock-market.bat" & Chr(34), 0
Set WshShell = Nothing

For a more detailed breakdown of the above script see ref[1] in the additional information section.

Running this file standalone will launch the above PowerShell file without displaying the window. We will use this file for the scheduled task action.

Windows Task Scheduler

Now we set up our scheduled task. You can open the task scheduler by a variety of ways (see ref[2]).

I use the Run feature by pressing Windows Key + R, and then launching taskschd.msc.

Create Task

Click on Create Task and when the box pops up, give it a name before moving to the triggers and actions.

Create Triggers

Create your triggers. Click on the Triggers Tab > New. I use Weekly under settings so that I can choose the days the event will activate.

Create Action

Then create your action. Click on the Actions Tab > New. This is where we will use the VBScript we created earlier.

Finish off by clicking Ok and creating your new scheduled task. Your audio should play at the times you specified when creating your triggers.

Additional Information