Exploring iTunes Using PowerShell: Part 1

I don’t spend a ton of time using iTunes on my laptop, but when I do it is usually to listen to some music or the iTunes radio and even that isn’t a common thing. But I happened to come across some information about a COM object within iTunes. Knowing this, I was able to connect to iTunes and perform some actions against it such as viewing the iTunes object, playing a file as well as manipulating the volume and a couple of other things.

Of course, you will need to have iTunes installed on your computer. Once you have done that it is just a matter of creating the connection to iTunes using the itunes.application COM object.

#Create iTunes Object
$itunes = New-Object  -ComObject iTunes.Application

iTunes will open up after you do this, so don’t be alarmed when the application all of a sudden pops open. Besides that, we can still start to explore the object and see what we can find.

image

Quite a few things here that are worth exploring to see what they hold. We can tell from the Version property that I am running iTunes Version 11.1.1.11.

We can play a file by using PlayFile() and supplying the full path to the file.

#Play a file
$itunes.PlayFile("C:\temp1 Head Like A Hole.m4a")

We can then view the current song information by looking at the CurrentTrack object.

#Show currently playing song information
$itunes.CurrentTrack

image

Lots of nice information! Everything that you could find by digging into the song file in iTunes can be found here for use. And as you can tell, a lot of this information can be edited if you see fit.

image

If you saw on the picture above this one, you would see Nine Inch Nails classified as Electronica/Dance (really?). So lets make a small change to what it really should read as.

$itunes.CurrentTrack.Genre = 'Industrial Rock'

image

image

Ok, lets, see if this is currently playing a song by checking the player state property. If it returns a 0, then that means that there is nothing playing (stopped or paused) while a 1 means that there is something currently playing at this moment.

#Player State; 0 -> Ready to play; 1 -> Currently Playing
$itunes.PlayerState

image

Looks like we still have a song playing; maybe I should pause this for a second so I can finish this example.

#Pause Song
$itunes.Pause()

image

Looks to not be playing now. Ok, now lets continue playing the song.

#Play
$itunes.Play()

Maybe I want the volume a little lower. To do this, I make use of the Volume property and give it a value of 0-100. I’ll drop it down to 75 in this case.

#Adjust Volume
$itunes.SoundVolume
$itunes.SoundVolume = 75
$itunes.SoundVolume

image

Cool! We can also mute and un-mute the volume using the Mute property and supplying a boolean value of True (Mute) or False (Un-mute).

#Mute iTunes
$itunes.Mute = $True

#UnMute iTunes
$itunes.Mute = $False

Lets skip to the next song now.

#Play Next track 
$itunes.NextTrack()
$itunes.CurrentTrack

image

If I wanted to go back a song, I simply do the following:

#Play previous track 
$itunes.PreviousTrack()

The last thing that I will show you is how to subscribe to a podcast and then update the subscription.

#Subscribe to a podcast
$iTunes.SubscribeToPodcast("http://feeds.feedburner.com/PowerScripting") 
 
#Update Podcast         
$iTunes.UpdatePodcastFeeds() 

I check my Podcasts folder in iTunes and sure enough, there is my new subscription.

image

Before I forget, you can quit iTunes using the Quit() method.

$itunes.Quit()

Being that this is a COM object, there is always the case for the memory to be released after use, so we will just do a little cleanup to ensure everything is good to go.

$null = [System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$itunes)
[gc]::Collect()
Remove-Variable itunes

As I continue to read the documentation on the SDK for the iTunes COM object and explore the object, it has become painfully obvious that not everything I want is publicly exposed for consumption. Even though, there is still more to cover in Part 2 as I will show you how to explore your playlists and play items from there as well as checking out what devices are currently connected to iTunes and adjusting the equalizer. So until then, thanks for stopping by!

About Boe Prox

Microsoft Cloud and Datacenter MVP working as a SQL DBA.
This entry was posted in powershell and tagged , , . Bookmark the permalink.

6 Responses to Exploring iTunes Using PowerShell: Part 1

  1. KERR says:

    I wonder if you could automate connecting to a shared library on the network too?

  2. Pingback: 5 AppleScripts To Whip Your DJ Library Into Shape - DJ TechTools

  3. KERR says:

    I wonder if there’s an easy way to export playlists, then re-import them if required?

  4. Pingback: Exploring iTunes Using PowerShell Part 2: The Equalizer | Learn Powershell | Achieve More

  5. Hey I noticed that .Artwork is stored as System.__ComObject I am wondering if there is a way to export the image as a .png? Or perhaps to load and then import an image file? Any ideas?

    • KERR says:

      I don’t know how to export the artwork using powershell, but I have used MP3Tag to scan MP3s in all folders and extract a folder.jpg for each folder in bulk. So you might be able to adjust the settings to do PNG files instead.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s