Updating your music collection with PowerShell

I’ve been banging my head against the wall on account of different music players which can’t play the subset of songs I have encoded as either Vorbis or AAC. So I’m slowly converting my entire music library over to MP3, which works everywhere, even if it’s less efficient.

But before I go digging through my old CDs I need to identify which albums I have to re-encode. My music is organized in folders by artist and then album, e.g.:

~\Music\Library\Pixies\Surfer Rosa\Where Is My Mind.mp3

So this means I effectively have to list the names of folders containing non-MP3 music files. Fortunately Windows PowerShell makes this a one-liner (though admittedly it’s a pretty long line; the backtick is PowerShell’s line continuation syntax):

ls -r Music\Library `
| ?{ $_.PSIsContainer -And ( $_.GetFiles() `
| ?{ $_.Name -Match "\.(m4[ap]|ogg|wma)$" } ) } `
| %{ New-Object PSObject -Property `
@{ Artist = (gi $_.PSParentPath).Name; Album = $_.Name } }

This will give you a nice list of non-MP3 albums, like:

Album              Artist
-----              ------
That's Your Fire   Aloha
Noble Beast        Andrew Bird
Believe It Mammals Bats & Mice
Charm School       Bishop Allen
[...]

And thanks to PowerShell’s object-oriented pipes, this is done without having to worry about any quoting or whitespace issues.

The more I learn PowerShell, the less I find myself relying on Cygwin for simple Windows administration tasks. I think Microsoft did a nice job balancing all the different requirements they had to meet with PowerShell. At the very least, it’s good to finally see a strong command shell built into Windows.