Windows – Play a WAV file with PowerShell and the .NET SoundPlayer Class

At work, they wanted to ability to play a soundbyte on a schedule as a reminder to our sales employees. I think I may have found this on stackexchange but I can’t quite remember at this point to give the full creds. A lot of the solutions I found tried to use Windows Media Player to play sound files or MP3’s, but unfortunately, there was no way to close the program after the file had played. This way uses Windows PowerShell and the .NET SoundPlayer Class to play the file.

In this example, if you want to play the first 25 seconds of a sound file (WAV files only, not MP3) and then exit, from the run prompt you can type this:

powershell -Command (New-Object Media.SoundPlayer "%SYSTEMDRIVE%\YourSoundFile.wav").Play(); Start-Sleep -s 25; Exit;

I then used Windows Task Scheduler to play the file once an hour as per their request. I’m not going to go over the complete steps on how to create a scheduled task and run it as a different user as there are plenty of tutorials out there to do that and I’ve even done a few here on other topics. I am simply just going to document the command line switches you need to set.

Action: Start a Program
Program/Script: "%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe"
Add Arguments (optional): -Command (New-Object Media.SoundPlayer "%SYSTEMDRIVE%\YourSoundFile.wav").Play(); Start-Sleep -s 25; Exit;

Leave a Reply