PowerShell – Automatically organizing your mp3-collection

Is your mp3-collection perfectly sorted? I know mine wasn’t. I thought I’d address that while creating a pretty good demo-script to show some basic filtering, script structures, etc. Since I also wanted the script to extract the metadata from every file in my collection I had to use the Shell.Application Com-object as well. All in all I think it turned out pretty well.

NOTE:

This script is provided completely as is. It is not an official product in any way and I take no responsibility for any harm it may cause.

Please note that certain metadata editors that integrate directly into the Shell may cause some severe confusion in the metadata, so I’d recommend disabling them before trying. Off course I would also recommend making a backup copy of the music library before surrendering it to the mercy of PowerShell.

Running the script

So, how do you run this script?

It’s quite easy. The first step would be to copy it into notepad save it with a .ps1 extension and open up PowerShell

Execution policy

If this is the first time you try to execute a script from PowerShell you will most likely have to change the execution policy for PowerShell. I’d recommend setting it to RemoteSigned. This (obviously) means that all remote scripts must be signed or PowerShell will refuse to execute them. To change the execution policy simply type:

Set-ExecutionPolicy RemoteSigned

That’s it.

If you’re uncertain what your execution policy setting is you simply type:

Get-ExecutionPolicy

Okay, having changed that we can now execute the script. If you saved the script in a folder that is in your system path you only need to type the filename of the .ps1 file. (The .ps1 is optional) So, if you saved it as RearrangeMP3s.ps1 you’d write RearrangeMP3s and press enter

Breakdown

Okay, so let’s take a look at the separate parts of the script.

Initial variables and constants

First we declare the parameters we’re going to use. In this case it’s just one: $Folder.

After that we have some constants. You may want to change $MUSICATTRIBS or $PLAYLISTATTRIBS to include, or remove, extensions based on your needs. $PLAYLISTSFOLDER is a constant which indicates in which subfolder you wish to store your playlists.

Finally we have some variables. $objShell is used for file system operations, and rather than creating a new object all the time I’d rather have a global one. $iTotalFiles and $iCurrentFile are two integers used to create a progress indicator.

Functions

After the initial variables come the sub functions called by the main function. To see what each individual function does I’d recommend looking at the comments. I’ve prioritized readability over minimum number of keystrokes, so yes, instead of writing:

The main function

Finally we have the main function. It is written last in the script. Why is that? Well, actually PowerShell is the most sequential language you’ll ever come across. It will not bother to check the entire script for function declarations. Instead it will go through the script one line at a time. This means that the following script will not work:

SayHello
function SayHello
{
Write-Host "Hello World"
}

When you call SayHello in the sample above the function has not yet been declared, so PowerShell will throw an exception in your face saying that SayHello is unrecognized.

Footnote

There is actually a supported way of achieving the very same thing, but where’s the fun in that?

Open up Windows Media Player

Go to Options -> Library and tick the checkbox for "Rearrange music in rip folder"

Go to the "Rip Music"-tab

Make sure the path is correct in the "Rip music to this location"-section

Click OK

Select all files in the library

Right-click and choose the "Advanced Tag Editor"-option

Do not change anything. Simply click Apply

Windows Media Player will now rearrange all the files using your Rip settings.