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.
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 184.108.40.206.
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
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.
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'
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
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()
Looks to not be playing now. Ok, now lets continue playing the song.
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
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
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.
Before I forget, you can quit iTunes using the Quit() method.
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!