Use PowerShell to Play Music Remotely

With Christmas right around the corner, the holiday spirit can be seen nearly everywhere you look. It can be seen in everything from the many Christmas lights and decorations to the pumpkin-spiced everything. It can even be seen in the snow (here in Utah, at least). In fact, here’s my buddy Chester getting his daily dose of holiday spirit. That’s some mighty fine holiday spirit.

corgi in snow

What does this have to do with PowerShell, you ask? Everything! As a system administrator (or even average IT guy), it’s your duty to get everybody into the holiday spirit by bombarding them with holiday excitement!

Try PDQ Deploy

How are we going to get everybody into the holiday spirit?! We’re going to blast our amazing Christmas music on everybody’s computers! How thoughtful of us!

Using PowerShell to Play Music Remotely

Are you excited yet?! Me too. Let’s tackle this by learning how to play sounds with PowerShell. Then, we’ll show you how to play these sounds remotely on your co-worker’s machines by using PDQ Deploy.

PowerShell and .wav files – SoundPlayer

(info link – System.Media.SoundPlayer)

This is one of the easiest ways to play a sound file via PowerShell. Sadly, it only support .wav files (see the notes box in the Remarks section – link).

$path = "C:\temp\Star Wars - Holiday Special sounds.wav"
 $soundplayer = New-Object Media.SoundPlayer $Path

We can even smoosh this together into a fairly simple one-liner:

(New-Object Media.SoundPlayer "C:\temp\Star Wars - Holiday Special sounds.wav").PlaySync()

If we want to use something fancier, such as an .mp3 file, we’ll have to use another option.

PowerShell for mp3s and more – Media.MediaPlayer

(info link – System.Windows.Media.MediaPlayer)

This method requires a little more setup, but it essentially works the same way.

Add-Type -AssemblyName presentationCore
 $filepath = [uri] "C:\temp\Futurama - Christmas Elves song.mp3"
 $wmplayer = New-Object System.Windows.Media.MediaPlayer
 Start-Sleep 2 # This allows the $wmplayer time to load the audio file
 $duration = $wmplayer.NaturalDuration.TimeSpan.TotalSeconds
 Start-Sleep $duration

Combine with PDQ Deploy – Music for Alllogo-deploy

Now that we know how to play audio via PowerShell, we can use these newly-discovered powers to blast coworkers with a hefty dose of holiday spirit in the form of Christmas music!

Since PDQ Deploy can deploy just about any software, script or patch to a machine (Yes. It’s that awesome), we’re going to utilize it for some fun.

All you need to do is create a PowerShell script (a .ps1 file) and use it in an Install Step:

powershell install file

If you’ve got a Pro or Enterprise license for PDQ Deploy, you can even set an option to only deploy to machine where a user is currently logged on. How delightfully convenient!

powershell logged on user

After that, you simply need to deploy this package to a machine where somebody is currently logged on and then watch the magic happen. While you’re at it, grab some popcorn and enjoy the show.

Nothing says, “Christmas” better than the confused look on someone’s face when seasonal music mysteriously starts playing on their speakers.

Final Notes

Now you can add musical depth to your scripts! Your creativity is the limit for applying music to your scripts.

Do you have a long script that wish that you you could notify you when it was done? Well, you probably already addressed it a long time ago after reading my blog post about sending emails. But, now you can play music/sounds in addition to sending emails! How delightful!

You could even loop music while your script runs to give your scripts background music!

Go forth and PowerShell!

Start a trial of PDQ Deploy

8 responses

  • My long running PowerShell scripts now have background musak thanks to and

    $wmplayer = New-Object System.Windows.Media.MediaPlayer
    Start-Sleep 2 # This allows the $wmplayer time to load the audio file
    $duration = $wmplayer.NaturalDuration.TimeSpan.TotalSeconds
        while ($stopwatch.Elapsed.Seconds -lt $duration)
            Write-Progress -Activity "Doing stuff, please hold..." -status "$($stopwatch.Elapsed.Seconds) seconds" -percentComplete ($stopwatch.Elapsed.Seconds / $duration*100)
            # do something
            # break when done
            start-Sleep -s 1
  • Tried doing what you put and this is what I got

    Start-Sleep : Cannot validate argument on parameter ‘Seconds’. The argument is
    null, empty, or an element of the argument collection contains a null value. Su
    pply a collection that does not contain any null values and then try the comman
    d again.
    At C:\Windows\AdminArsenal\PDQDeployRunner\service-1\exec\letitgo.ps1:8 char:13
    + Start-Sleep <<<< $duration
    + CategoryInfo : InvalidData: (:) [Start-Sleep], ParameterBinding
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power

    What do I need to do to get this working?

    • That error is saying that the $duration value is $null. You can simulate this error by typing, Start-Sleep $null in a Powershell console.

      Quick fix: try adjusting the Start-Sleep step from the value of 2 to larger values.

      Why? It all depends on how long it takes your target system to open the $filepath for the file — larger media files and slower machines will require more time. The $wmplayer has to fully open the file in order to determine the duration. That $duration keeps the Powershell process running long enough to finish playing your media file.

      I used 2 seconds since that appeared to work with all the files that I tested on my machines. I would suggest trying to increase the sleep time to allow the $wmplayer more time to load the audio file. Perhaps start with Start-Sleep 5 and see how that works.


    • You’re absolutely right. I just scanned over my script and my screenshots and I realized that there is a bit of a disconnect. Who knows where my brain was that day. 🙂

      There are a few ways to tackle this.

      You could reference the mp3 file directly from a UNC path within your script or you could use a relative path and copy it over with your PDQ Deploy package as an Additional File for the step.

      Good eye and thanks for the comment!

Your email address will not be published.

Your Name

This site uses Akismet to reduce spam. Learn how your comment data is processed.