Playing a Sound on Error

by Jan 29, 2014

To catch a user’s attention, your script can easily play WAV sound files. Here is a simple function:

function Play-Alarm {
    $path = "$PSScriptRoot\Alarm06.wav"
    $playerStart = New-Object Media.SoundPlayer $path

It assumes the WAV file is located in the same folder the script is saved in. Note that $PSScriptRoot is not supported in PowerShell 2.0.

Just make sure you rename the $path variable and have it point to any valid WAV file you want to use.

By default, PowerShell will wait until the sound is played. Replace PlaySync() with Play() if you want PowerShell to continue and not wait.

Twitter This Tip! ReTweet this Tip!