[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 two of a four part series of articles about using VBScript to work with the Windows Media Player object model. Part one began the introduction to the object model.

Continuing from yesterday. For now, let’s see how scripts can help us manage our music files and playlists.

Retrieving All the Items in Your Media Collection

In old Disney cartoons Scrooge McDuck used to sit in his private bank vault, surrounded by every last dollar he owned. As a digital media aficionado you’ll often want to do the same thing: you’ll want to be able to retrieve a list of every last piece of digital media you own. Here’s a script that will give you a Scrooge McDuck experience (though, unfortunately, it’ll just be with your digital media files and not with a bank vault full of money):

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set colMediaCollection = objPlayer.mediaCollection

Set colMedia = colMediaCollection.getAll()

For i = 0 to colMedia.Count - 1

Set objItem = colMedia.Item(i)

Wscript.Echo objItem.Name


As you can see, it’s actually a very simple script. We begin by creating an instance of the WMPlayer.OCX object. This is the parent object for all Windows Media Player scripts; all the other objects, collections, and arrays we’ll use are derived from this object. After we create the Windows Media Player object we can then create an instance of the MediaCollection object by using this line of code:

Set colMediaCollection = objPlayer.mediaCollection

Now, how do we get at all the items stored in the media collection? The easiest way to do that is to use the getAll() method; as the name implies, this returns an array consisting of all the items in the collection. And because this is an array, we can use a For-Next loop to loop through all the items in the array (and thus through all the media items in our collection):

For i = 0 to colMedia.Count - 1

Set objItem = colMedia.Item(i)

Wscript.Echo objItem.Name


Why does our loop run from 0 to the number of items in the list (colMedia.Count) minus 1? Because arrays begin with item 0 and continue through the number of items minus 1. For example, a 5-item array would be numbered like this:


Item Number

First item


Second item


Third item


Fourth item


Fifth item


Inside the For-Next loop we create an object reference to the individual item and then simply echo back the Name of that item. Are there other properties we could retrieve besides the Name? You bet there are, and we’ll discuss those momentarily. (If you just can’t wait, then skip ahead to the section titled Working with Media Object Attributes.)

Retrieving Items by Item Type

The preceding script shows you all the items in your Media Collection, including songs, playlists, videos, and what-have-you. There are times when you might find it useful to retrieve all these items; at other times, however, you might want to return only a subset of items, maybe all the music files or all the video files. Using the Windows Media Player object model you can write a script that returns only:

Audio items

Video items

Photo items


Other items that might be found in the collection

For example, the following script returns only the audio items found in the media collection. The script begins by creating instances of the WMPlayer.OCX and MediaCollection objects, then uses the getByAttribute method to return all the items with the MediaType of Audio. The Audio items will be returned as an array, so the script uses a For-Next loop to walk through all the items in that array, binding to each song and then echoing the song Name.

The script itself looks like this:

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set objMediaCollection = objPlayer.MediaCollection

Set colSongList = objMediaCollection.getByAttribute("MediaType", "Audio")

For i = 0 to colSongList.Count - 1

Set objSong = colSongList.Item(i)

Wscript.Echo objSong.Name


The focus of this article is music files; we aren’t going to talk in any detail about other media items. (For more information about other media types, click here.) However, to better illustrate how the getByAttribute method can be used to retrieve other media items, the following script returns the name and dimensions of all the photo items in the Media Collection:

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set colMediaCollection = objPlayer.mediaCollection

Set objPhotos = colMediaCollection.getByAttribute("MediaType", "photo")

For i = 0 to objPhotos.count - 1

Set objPhoto = objPhotos.Item(i)

Wscript.Echo "Name: " & objPhoto.Name

Wscript.Echo "Height: " & objPhoto.getItemInfo("WM/VideoHeight")

Wscript.Echo "Width: " & objPhoto.getItemInfo("WM/VideoWidth")


By default, Windows Media Player doesn’t automatically keep track of photo items. To add .JPG files to your media collection you can either use a script or, in the Player itself, click File and then select one of the options under Add to Library. Before you can add .JPG files to Windows Media Player 10, however, you must first click Tools, click Options, and then, on the Player tab, select Enable picture support for devices.

Working with Media Object Attributes

As we hinted at earlier, media objects have more attributes than just their name. For example, audio items have attributes such as:

Album (Album)

Artist (WM/AlbumArtist)

Year of release (WM/Year)

Number of times played (UserPlayCount)

Working with attributes can be a little tricky; that’s because you typically have to work with two different sets of attributes. To begin with, the Media object has a set of attributes that are applied to all media items regardless of type; these attributes include such things as Name, durationString (the playing time for the item, in the format minutes:seconds), and sourceURL (the path to the media file). The values of these attributes can be directly reported, using code similar to this:

Wscript.Echo objItem.durationString

Wscript.Echo objItem.sourceURL

Other attributes, however, are tied to particular media types; for example audio items have a different set of attributes from photo items, which have a different set of attributes from video items. To access these attributes you must use the getItemInfo method, passing getItemInfo the name of the attribute you want to retrieve. For example, these two lines of code use getItemInfo to echo the values of the WM/AlbumArtist and UserPlayCount attributes:

Wscript.Echo objSong.getItemInfo("WM/AlbumArtist")

Wscript.Echo objSong.getItemInfo("UserPlayCount")


Note. So how are you supposed to know that WM/AlbumArtist and UserPlayCount are attributes of an audio item? The best way that we know of is to browse the Windows Media Player SDK on MSDN.

Incidentally, many of these attributes are read/write; that means you can use a script to change the attribute values. We aren’t going to talk about modifying attribute values in this article. However, here’s a sample script showing how the setItemInfo method can be used to change the Name of a music file:

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set objMediaCollection = objPlayer.MediaCollection

Set objTempList = objMediaCollection.getByName("She Bop [Album Version]")

Set objSong = objTempList.Item(0)

objSong.setItemInfo "Name", "She Bop"

That is all there is to using VBScript to work with Windows Media Player collections Next weekend we will post the remaining 2 parts. Join me tomorrow as I begin a new week on the TechNet Script Center. I invite you to follow me on Twitter or Facebook. If you have any questions, send e-mail to me at scripter@microsoft.com or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy