Weekend Scripter: Organize Music Collection with PowerShell

Doctor Scripto

Summary: Use Windows PowerShell and free utilities to clean up your directory structure and organize MP3s.

Honorary Scripting Guy, Sean Kearney, is here. Today I was staring at a big pile of MP3s on my computer. It was actually a consolidation from several different machines. Old Media Player stuff, junk from a previous iTunes installation from years back, some things my kids had. There are probably duplicates and untagged MP3s. And add to that…

I had some from a Windows Home Server that had a drive system go “funny” because I (in my great and powerful wisdom as an ITpro who would try anything once) virtualized Windows Home Server.

And right now…I can hear members of the design team for Windows Home Server laughing their posteriors to the bone. Here is the advice from Microsoft with the first release of Windows Home Server, “Don’t virtualize this, the file system can go corrupt.”

But I knew better.

OK, I guess I didn’t. So the result was MP3s from that computer had some good audio, but the wrong file name—and…well…

We have a mess.

So I decided to dump it all to an external 500 GB USB 3.0 drive and have at it.

My first challenge was getting rid of all the rubbish. There were old JPG files and INI files from older audio players.

To be quite honest, the only thing I cared about were MP3 files. So for this, I called up my “Sonic Screwdriver” of utilities: Windows PowerShell.

First find me all of the files that are not MP3:

Get-Childitem G:\MusicPile –exclude *.mp3 –recurse

Now get rid of them. But I’ll play it safe first. Show me what you’re going to do:

Get-Childitem G:\MusicPile –exclude *.mp3 –recurse | Remove-Item –whatif

Quickly scanning the output and seeing no random MP3 files in there, I “released the Kraken”:

Get-Childitem G:\MusicPile –exclude *.mp3 –recurse | Remove-Item

Ahhhhh…much better. Only music. Much more drive space too!

Next I decided to get rid of all of the duplicate MP3 files and cut most of my work down. There are a lot of great versions to pay for, but because I was working on a “restrictive budget” for this task (OK, I’m cheap!), I dug through SourceForge and the open-source community.

What I found was a nice little utility called dupeGuru Music Edition, which exists from community contributions.

Trying it costs nothing, but after using it, I highly recommend throwing some cash their way. The application works well and quite fast. There are some better ones that leverage hashing methods to find files that are a close match, but this one was simple, and it “just worked.”

All I did was run the application, specify a target folder, and click Scan.

Image of menu

It took maybe one minute to scan 9,000+ files and produce the results for what I needed to do. I clicked the Dupes only check box, selected Mark on the main menu, and chose Select All:

Image of menu

Now the cool (and safe part)…

Under Actions, you have a good selection aside from Delete or “run off to the side room crying and screaming.” You can simply Move them out of that folder structure into a new one:

Image of menu

I moved the duplicates to a folder called G:\IThinkTheseAreDupes.

Now with a cleaner and leaner structure, I went to the next task: trying to make sure the file content (the actual audio) reflected the song, and cleaning up the file names properly.

For this, I ran across a free service years ago called MusicBrainz . MusicBrainz is an audio database that takes the actual sound and compares it against a known database to help tag and name files properly.

To enable this, I obtained a client application called “Picard,” which has served me well before. OK, maybe just the title is the part I find cool as I keep thinking, “Star Trek! Make it so!”

Picard isn’t hard to use (another reason I like using it). First launch the program, then choose Add Folder, and browse to your choice folder of MP3 goodies.

Image of menu

Next I went to the options of the application to customize how it would deal with my files. I wanted the files renamed when I saved them to the default format from Picard, and I wanted them moved to a brand new structure. So as my music was identified, it would be successfully saved into a clean new directory structure.

So I right-clicked Unmatched Files, and  then clicked Scan:

Image of menu

The next part takes quite a while because it scans the MusicBrainz database and compares my MP3s with its known collection.

Image of menu

After it has built a large pile in the right window, and you see Pending Files at 0 on the bottom of the screen, you can select all the titles on the right to begin the tagging and renaming process. (This also places them into proper folders in the new structure.)

Then on to the final task…

There was stuff left in the old music structure that Picard didn’t know about and left alone. But there was still a chance of empty directories. Here is where Windows PowerShell steps in again.

I wanted to identify all of the folders that were directories that had no files and no directories.

There are two methods I can leverage with Get-ChildItem. There is GetDirectories() and GetFiles(), which I can apply to the object. One of the properties I can leverage is Count.

So if GetDirectories() has a count of zero and GetFiles() has a count of zero, that will be the criteria for an empty folder!

Get-Childitem G:\Music –Recurse –Directory | where { $_.getdirectories().count –eq 0 –and $_.getfiles().count –eq 0 )

Cool! It worked. Now to remove them safely first:

Get-Childitem G:\Music –Recurse –Directory | where { $_.getdirectories().count –eq 0 –and $_.getfiles().count –eq 0 ) |
Remove-Item –path $_.Fullname –whatif

Satisfied that there was nothing left but empty folders, I pulled off the safety switch:

Get-Childitem G:\Music –Recurse –Directory | where { $_.getdirectories().count –eq 0 –and $_.getfiles().count –eq 0 ) |
Remove-Item –path $_.Fullname

Moments later, I had a new clean folder structure that was well organized!

For later music?

Digging through the Internet and Music Brainz, there are some good “for pay” solutions that will do an even more granular task of identifying the music with similar databases. But for me, this was a good start.

If you play with the applications for MusicBrainz, you’ll even find some fresh ways to update your album photos.

This solution is, of course, not for everybody. It was good for me. It was fun to do, and I thought I’d share.

Enjoy the weekend! I’m off to load up my Windows Phone with some Jethro Tull.

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send an email to the Scripting Guys at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then just remember, the Power of Shell is in You.

Sean Kearney, Windows PowerShell MVP and Honorary Scripting Guy


Discussion is closed.

Feedback usabilla icon