Use Powershell to Play Random MP3s

Date: 27 November 2019

I’ve been playing with tools to help make my game streams easier. One of the things that I wanted to do was to play random MP3 from a directory. Here’s a little Powershell to get it done.

Here’s the powershell. Drop this in a file named play-random.ps1.



Import-Module Microsoft.PowerShell.Management

$file = gci -Path "$directory\*.mp3" | Get-Random -Count 1

& $mpg123 $file

The first thing you need is mpg123. You can install this anywhere but you need to know the path to mpg123.exe. You can use that to replace the path that is set in the variable $mpg123.

I have a directory named win that contains a bunch of MP3s of movie quotes that I play on the rare times I win a round of Apex Legends. That’s passed to the -directory argument.

powershell.exe -NoLogo -NonInteractive -WindowStyle Hidden -executionpolicy bypass -File play-random.ps1 -directory win

That’s great. Huzzah and stuff. Now, I’d like to be able to run this at the touch of a button. There are a number of ways to do that. I can create a hot key for each directory in OBS Studio but even better are tools like Up Deck or Mix It Up. The problem with both of those is they don’t play well with the arguments.

To get around that, I had to create a simple batch file for each directory, like so.

chdir "C:\Users\username\audio-clips\"
powershell.exe -NoLogo -NonInteractive -WindowStyle Hidden -executionpolicy bypass -File play-random.ps1 -directory win

Ooo, aaah. Now all that needs to be done is to configure a button that runs the batch file. Easy peasy lemon squeezy.