Weekend Scripter: Use Windows PowerShell to Play Recorded Music

Summary: Use a hash table and an array to run prearranged actions.

Honorary Scripting Guy, Sean Kearney here. I’m filling in for our good friend, Ed again today. I think he’s busy playing some tunes with the Windows PowerShell piano we created last week.

So continuing from last weekend’s silliness, I had the song “Popcorn” in my head. I was thinking, “Is there any way we could have some prerecorded music for Windows PowerShell?”

Well, this is definitely something we can do, but we need to figure out a few details to play music. Looking at a typically sheet of music, we have some details to be aware of:

Note  A note (A,B,C,D) could be sharp, flat, or neither (let’s say, neutral).

Octave  Our note would be an octave, and there can be multiple C notes at different frequencies.

Time  Timing could be quarter, half, or whole (even eighth or sixteenth, but we’ll start simple). The note could also be “dotted” (plays 50% longer than its variant).

Beats  Each measure plays a certain number of beats at a time (4/4 is what we’ll work with).

So before Ludwig Von Beethoven comes out to correct my musical errors, we’re going to define a special array. We’re going to change our original music array from last week slightly.

First, instead of a key on the keyboard, we’re going to create a new pattern for musical notes. We’re going to identify octave, note, and sharp or flat with this simple designation:

# First Column Identifies Octave (A-C) (where C is the highest) - Octave 0 = REST

# Second Column Identifies Note

# Third Column Designates Note as Sharp "S" or Not "N"

For a C note in the second octave, it will look like this:


A D# in the third octave looks like this 


And of course, a rest note will have a special designation of RRR. So our list of notes will translate to this:
























After that, we need to define some particular details for notes and duration. For time, we’ll base everything on 4/4 and over two blocks of measures:

Eighth note       8

Quarter note      4

Half note           2

Whole note       1

To plug in a dotted note, we’ll add an additional column, 1 or 0 for dotted or non-dotted. 

So to play an F#, second octave, whole note, non-dotted, we would use this:


Or to play a G note, third octave, half note, dotted, we would use this: 


So for our data array, the first three entries will contain the note, and the last pair is how long we’ll play it. Let’s plug-in some data directly:

# Whole Note Dotted





Cool. So let’s put this together as a small array to play “Popcorn”:






We’ll create a loop to parse the data, and pass the results through to the array to determine the note and duration:




do {










With this data in hand, let’s play the music:

If ($Note -ne "RRR")

{START-SLEEP -milliseconds (600*$Playtime)}



} Until ($Start -ge $End) 

TaDa! You’re a musician with Windows PowerShell!

If you’d like to download and listen to this silly little script, it’s sitting in the TechNet Gallery: Windows PowerShell Popcorn Tune.

You can adapt the music array to meet your own needs, including speeding it up and optimizing it (maybe even tune my horridly guessed notes).

Tune in tomorrow when we use Windows PowerShell to cook up a little popcorn for our script.

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Sean Kearney,
Honorary Scripting Guy and Windows PowerShell MVP

Comments (5)

  1. David Wyatt says:

    Fun stuff.  🙂  The dotted-note logic doesn't look right, though.  I think rather than this:


    You probably wanted this:

    $PlayTime = 4 * (1 / $length) + (.5 * [int]$Dotted)

  2. David Wyatt says:

    Actually, now that I look closer, none of us has it quite right yet.  JRV was closest, but was multiplying a string by a double, with the string on the left, and not casting it to a number first.  Either reversing the order (0.5 * $data.substring(4,1)) or casting $data.substring(4,1) to [int] would fix that.

    My original post was adding a flat 0.5 to the length, not actually multiplying the length by 1.5 (which seemed to be working, because my tests happened to be for values that came out as 1 and 1.5).

    Sean, from what you say you intended, it looks like the main problem was using "=" instead of "-eq" (though the math still needs a slight tweak, see below).

    Here's what I should have posted the first time:

    $PlayTime = 4 * (1 / $length) * (1 + .5 * [int]$Dotted)

  3. jrv says:

    @Sean – I see math is not one of your strong points;)

    $dotted=1 + ($data.substring(4,1) * .5)

    This is how to prevent a zero in a product list.  This will always be either 1 or 1.5.  This is one way to get a sustain or dotted note option when the options are binary (1/0,true/false)

    It looks like you were either trying to do that or possibly posted the wrong version.  I think doing it at the extraction is more explicit.

  4. @david

    My intent was to pull a Binary True and convert to integer but you've found a better method! Cool!


    I love it!  This was I think my first idea (and I pulled this from a working script) but you're absolutely correct, there was DEFINITELY a better way 🙂


  5. @jrv and @david

    BTW, it appears I am the victim of a keyboard sneeze or SOMETHING odd 😛

    I checked my original script, it had the correct formula (+ instead of * in the Math function)

    Chalk one up for Mr. Murphy 😉


Skip to main content