How to use PowerShell to play music remotely

Black and White PDQ logo
Kris Powell|December 21, 2015
Use PowerShell to Play Music Remotely
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.

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!

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 $soundplayer.PlaySync()

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

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 $wmplayer.Open($filepath) Start-Sleep 2 # This allows the $wmplayer time to load the audio file $duration = $wmplayer.NaturalDuration.TimeSpan.TotalSeconds $wmplayer.Play() Start-Sleep $duration $wmplayer.Stop() $wmplayer.Close()

Combine with PDQ Deploy means music for all

PDQ Deploy icon

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 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!

Black and White PDQ logo
Kris Powell

Kris was an employee at PDQ.

Related articles