The Scripting Guys Really Do Rock, Part 1 of 4


[Disclaimer: this is a reprint of a previously published article from the TechNet Script Center FunZone. The FunZone was not migrated from our old platform. This article is printed here due to popular demand.]

This is part one of a four part series of articles about using VBScript to work with the Windows Media Player object model.

Using Scripts to Manage Your Music Files and Playlists


There’s no doubt that being a music connoisseur is much more complicated today than it was a few years ago. For example, it wasn’t all that long ago that managing your music collection involved nothing more than putting your record albums or CDs in alphabetical order. Like they say, not exactly rocket science.

Today, of course, things are very different. A few years ago your big chore might have been sorting through 50 CDs looking for the one you wanted to play; today you might need to sort through hundreds or even thousands of digitized music files looking for individual songs you want to play. At one time it was easy to construct a “playlist;” after all, a playlist was simply the songs found on one side of a record or on a CD, and in whatever order they happened to be recorded. Today people want to construct custom playlists featuring songs taken from different albums and recorded by different artists. Needless to say, managing digital music files can be both tedious and time-consuming, and almost makes you long for the days when the biggest decision facing music lovers was whether to play side 1 or side 2 of a cassette tape.


Microsoft’s solution for helping deal with the digital music era is Microsoft Windows Media Player. Windows Media Player is a great tool for playing digital music, and it’s a pretty good tool for organizing your music files; you can view songs by artist, by album, by genre, etc. Not only that, beginning with Windows Media Player 10 it lets you “rip” music from CDs or – alternatively – burn digital music files to CDs. You can use Windows Media Player to watch digitized video or use it to synch up with your portable music player. Not too shabby, huh?

Of course, to be entirely fair, Windows Media Player does have it weaknesses. For example, Windows Media Player is a great tool for viewing your media collection. But what if you want to print a copy of your song list? What if you want to copy the song information to a database or spreadsheet? In cases like that you’re out of luck; Windows Media Player includes neither an Export function nor a Print button.

But that’s OK. Windows Media Player might not have a Print button, but it does have a very rich object model, which means you can script many of these features. Want to create a new playlist on-the-fly and have Windows Media Player play the new collection of songs? No problem; that can easily be scripted. Want to grab information about all your digitized songs and copy that data to an Excel spreadsheet? Again, no problem; that can also be easily scripted. In fact, it turns out that scripts can be very useful in helping you manage your music collection and playlists. In this article we’ll introduce you to the Windows Media Player object model and provide you with a number of scripts to help you manage your digital music collection.

Or at least we will right after we finish listening to Let’s Spend the Night Together, by the Rolling Stones….

Three notes. First, the scripts used throughout this article were tested for Windows Media Player 10. Will the scripts work on previous versions? Probably, but we can’t say that for sure. For better or worse, you’re on your own if you’re still running, say, Windows Media Player 7.

Second, the scripts used in this article not only require Windows Media Player to be installed on your computer but also assume that you have already added your music files to the Player’s media collection. If you haven’t done the latter, a script included in this article shows you how you can do that. Alternatively, you can start Windows Media Player, click File, and then select one of the options listed under Add to Library. Note that this doesn’t move your files to a new location on the hard drive and it doesn’t somehow configure them so they can be played only with Windows Media Player; it just makes the Player aware of the files’ existence.

Finally, one of our testers occasionally ran into problems when running a script while Windows Media Player was open. We couldn’t always re-create those problems, but it’s probably best to close Windows Media Player before running any of the scripts included in this article.

Working with the Windows Media Player Object Model


We’ll warn you right up front: at first glance the Windows Media Player object model can seem a little daunting. Part of that is due to its comprehensiveness: there are objects for your media collection and for your playlists and for individual music files and for CDs and for DVDs and – well, you get the idea. You can do quite a bit with Windows Media Player, although – for this article – we’re going to concentrate on managing music files and playlists.

In addition to its depth and breadth, however, the Windows Media Player object model is also complicated by the fact that objects imported into the Player are not required to have unique names. We wouldn’t advise it, but if you wanted to you could have 100 songs called My Favorite Song or 100 playlists called My Favorite Playlist. This becomes a problem for script writers simply because you can’t search Windows Media Player for My Favorite Song and be connected to the one and only copy of My Favorite Song; that’s because you might have 500 different songs all named My Favorite Song. As a result, when you query the Player for an item you invariably get back an array of items. More often than not that array will consist of just one item, but it’s an array nonetheless.

Because of that you will need to bind to an individual item within that array before you can work with it. Throughout this article you’ll see code similar to this:

Set objAll = objPlaylists.getByName(“Dire Straits Playlist”)

Set objList = objAll.Item(0)

In this sample code, we’ve retrieved an array of playlists with the name Dire Straits Playlist. We’re assuming that your playlists have unique names; thus there will be only one such playlist in the array. That’s why, in line 2, we bind directly to Item(0), the first item in the playlist. If you really do have multiple playlists with the same name you will need to use a For-Next loop to walk through all the items in the array. It’s a kind of oddball approach and something you probably aren’t used to. But it is what it is.

As for the objects themselves, you’ll generally find yourself dealing with the following:




This is the object representing the Windows Media Player itself. This is also the parent object for all the other Windows Media Player objects. In other words, you need to create this object before you can create – and thus work with – any other objects.


The MediaCollection object represents all the items stored in the Windows Media Player media collection. You will typically query the MediaCollection object to return collections of media items. For example, you might query the MediaCollection object to return a list of all your Beatles songs or all the songs originally released in 1989.


The PlaylistCollection object represents all the music playlists stored in the media collection. To work with an individual playlist you will typically query the PlaylistCollection and retrieve the desired playlist.


A playlist is simply a list of songs to be played as a group. The Windows Media Player object model allows you to load, create, delete, and modify individual playlists.


Each individual item stored in the media collection is a Media object. Media objects can include audio files, photographs (.JPG files), music playlists, video files, and other media items.

But don’t worry too much about all that; you’ll catch on quick enough.

That is all there is to using VBScript to work with the Windows Media Player object model. Join me tomorrow as we continue this article and begin writing scripts. I invite you to follow me on Twitter or Facebook. If you have any questions, send e-mail to me at or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Discussion is closed.

Feedback usabilla icon