Use PowerShell to Easily Organize Your Music Collection

Use PowerShell to Easily Organize Your Music Collection

  • Comments 2
  • Likes

Summary: Microsoft Scripting Guy, Ed Wilson, teaches you how to use Windows PowerShell to organize your music collection for ease of playback on Windows.

Microsoft Scripting Guy, Ed Wilson, is here. I love the weekend. In general, it is a time for me to explore and to experiment with Windows PowerShell. These experimentation sessions quite often are purpose driven—I have some annoying problem that has finally reached a sufficient threshold of vexation to merit a few time slices of my day.

I enjoy playing music on my computer while I am writing. Often I listen to my Zune, but it is easier for me to use Windows Media player. There is little performance hit on my system because my music files are on a separate drive than my operating system or my Hey, Scripting Guy! data files. The problem with the manual method of playing music files is that the songs reside in dozens of individual, nested folders. This layout is shown in the image that follows.

Image of folder

For manual music operations, a better arrangement (at least for me) is to have all the songs in a single folder. With this arrangement, all I need to do is select the songs I want to listen to, and then I can select Play directly from Windows Explorer. This technique is shown in the following image.

Image of folder

The cool thing about Windows PowerShell is that I can fix annoying problems, such as the one I have with my music files, without the need to write a script. In fact, it is three commands to fix this issue. The first command sets my working location to the G:\music folder. The next command moves all of my music files to the root of the Music folder. I use the Get-ChildItem cmdlet (gci is the alias) to retrieve a listing of all the MP3 files in my Music folder. I use the Foreach-Object cmdlet (% is the alias) to move each file by using the Move-Item cmdlet (move is alias) to the root of my Music folder. Here is the command.

sl g:\music

gci -Filter *.mp3 -recurse | % {Move -Path $_.fullname -dest g:\music}

Note: For my particular application, this technique works just fine. If you have files outside of nested folders, you will need to add a check for the parent folder, or else the command will attempt to move the file to the location where the file already exists, and it generates errors.

If you are not certain what a command will actually do, make sure that you use the WhatIf parameter. Here is the command, revised to incorporate the WhatIf parameter.

gci -Filter *.mp3 -recurse | % {Move -Path $_.fullname -dest g:\music -whatif}

Now, I need to delete all the empty folders. To do this, I use two commands. The first command uses the Get-ChildItem (gci is alias) to find all of the child folders. I use the Recurse parameter to force the command to find folders and all subfolders. To identify a folder, I use the Where-Object (? is the alias) to find the Mode that begins with the letter d. Here is the first command to find all the folders.

$dir = gci g:\music -recurse | ? {$_.mode -match '^d'}

Now I need to find the folders that are empty. I pipe the collection of FolderInfo objects to the Where-Object (? is the alias), and I call the GetFiles method to see how many files exist in each folder. If there are no files, I pipe the object to the Remove-Item (del is the alias) cmdlet. Here is the second command.

$dir | ? { !($_.getfiles().count) } | del

Once again, if you are not certain as to what the command will accomplish, add the WhatIf parameter to the Remove-Item command. Here is the command that uses the WhatIf parameter.

$dir | ? { !($_.getfiles().count) } | del -whatif

Cool, I now have my music collection in a single folder, and I can use Windows Media player to play directly from within Windows Explorer. Sweet! Join me tomorrow when I will welcome Microsoft PowerShell MVP, Sean Kearney, as he begins a special week in Blueville. It is great stuff!!! See you!

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Congratulations, you just removed any organisation at a file system level that you did have.

    Seriously now how are going to play the Black album from Metallica bit nothing else after you do this?

    I was hoping this might be a useful post about duplicate detection or reading ID3 tags.

    Well below your usually excellent and useful post quality.

  • @GavinB You are correct; I wanted to remove the filesystem organization to my music files. Keep in mind, this is a special purpose application. In fact, this is a copy of my actual music collection. I would not do this to the orginal music files that I sync with my Zune. I simply wanted to be able to not have to search through hundreds of folders to pick out a few songs to play. As to your question about how am I going to find the Black album from Metallica? This is simple. I first customize my folder in Windows Explorer to be Music. This adds artist, album, etc. columns to my view. I can choose to display additional meta data if I wish. I can then click the column heading, and Voila, I have a sort by albumn. I have a number of articles about working with music files meta data, and one specifically that talks about ID3 tags (it is written in VBScript, but would be easy to translate) here is that link http://bit.ly/vh7aRb  Here are the PowerShell and multimedia articles http://bit.ly/tT5OK6 I hope this helps. Thank you for keeping me honest. Keep in mind, this technique of flattening a directory structure is applicable for many different purposes. The cool thing is I did not have to write a script to do this.