Playing Sound in the Background

by May 28, 2015

If your script takes some time to complete, you may want to play a system sound file. Here is a sample illustrating how this can be done:

# find first available WAV file in Windows
$WAVPath = Get-ChildItem -Path $env:windir -Filter *.wav -Recurse -ErrorAction SilentlyContinue |
Select-Object -First 1 -ExpandProperty FullName

# load file and play it
$player = New-Object Media.SoundPlayer $WAVPath

  'Doing something...'
  1..100 | ForEach-Object { 
    Write-Progress -Activity 'Doing Something. Hang in' -Status $_ -PercentComplete $_
    Start-Sleep -MilliSeconds (Get-Random -Minimum 300 -Maximum 1300)


The sample script takes the first WAV file it finds in the Windows folder, and plays it for as long as the script performs some action. You can of course provide a path to any other WAV file.

Twitter This Tip! ReTweet this Tip!