{"id":11751,"date":"2011-12-18T00:01:00","date_gmt":"2011-12-18T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/12\/18\/use-powershell-to-easily-organize-your-music-collection\/"},"modified":"2011-12-18T00:01:00","modified_gmt":"2011-12-18T00:01:00","slug":"use-powershell-to-easily-organize-your-music-collection","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-easily-organize-your-music-collection\/","title":{"rendered":"Use PowerShell to Easily Organize Your Music Collection"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, teaches you how to use Windows PowerShell to organize your music collection for ease of playback on Windows.<\/p>\n<p>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&mdash;I have some annoying problem that has finally reached a sufficient threshold of vexation to merit a few time slices of my day.<\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7701.hsg-12-18-11-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7701.hsg-12-18-11-01.png\" alt=\"Image of folder\" title=\"Image of folder\" \/><\/a><\/p>\n<p>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 <b>Play<\/b><i> <\/i>directly from Windows Explorer. This technique is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2437.hsg-12-18-11-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2437.hsg-12-18-11-02.png\" alt=\"Image of folder\" title=\"Image of folder\" \/><\/a><\/p>\n<p>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 <b>Music<\/b><i> <\/i>folder. I use the <b>Get-ChildItem<\/b> cmdlet (<b>gci<\/b> is the alias) to retrieve a listing of all the MP3<i> <\/i>files in my <b>Music<\/b> folder. I use the <b>Foreach-Object <\/b>cmdlet (<b>%<\/b> is the alias) to move each file by using the <b>Move-Item <\/b>cmdlet (<b>move<\/b> is alias) to the root of my <b>Music<\/b><i> <\/i>folder. Here is the command.<\/p>\n<p style=\"padding-left: 30px\">sl g:\\music<\/p>\n<p style=\"padding-left: 30px\">gci -Filter *.mp3 -recurse | % {Move -Path $_.fullname -dest g:\\music}<\/p>\n<p><b>Note<\/b>: 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.<\/p>\n<p>If you are not certain what a command will actually do, make sure that you use the <i>WhatIf <\/i>parameter. Here is the command, revised to incorporate the <i>WhatIf <\/i>parameter.<\/p>\n<p style=\"padding-left: 30px\">gci -Filter *.mp3 -recurse | % {Move -Path $_.fullname -dest g:\\music -whatif}<\/p>\n<p>Now, I need to delete all the empty folders. To do this, I use two commands. The first command uses the <b>Get-ChildItem<\/b> (<b>gci<\/b> is alias) to find all of the child folders. I use the <i>Recurse <\/i>parameter to force the command to find folders and all subfolders. To identify a folder, I use the <b>Where-Object <\/b>(<b>?<\/b> is the alias) to find the <b>Mode<\/b><i> <\/i>that begins with the letter <b>d<\/b><i>.<\/i> Here is the first command to find all the folders.<\/p>\n<p style=\"padding-left: 30px\">$dir = gci g:\\music -recurse | ? {$_.mode -match &#8216;^d&#8217;}<\/p>\n<p>Now I need to find the folders that are empty. I pipe the collection of <b>FolderInfo<\/b> objects to the <b>Where-Object <\/b>(<b>?<\/b> is the alias), and I call the <b>GetFiles<\/b><i> <\/i>method to see how many files exist in each folder. If there are no files, I pipe the object to the <b>Remove-Item <\/b>(<b>del<\/b> is the alias) cmdlet. Here is the second command.<\/p>\n<p style=\"padding-left: 30px\">$dir | ? { !($_.getfiles().count) } | del<\/p>\n<p>Once again, if you are not certain as to what the command will accomplish, add the <i>WhatIf <\/i>parameter to the <b>Remove-Item <\/b>command. Here is the command that uses the <i>WhatIf <\/i>parameter.<\/p>\n<p style=\"padding-left: 30px\">$dir | ? { !($_.getfiles().count) } | del -whatif<\/p>\n<p>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!<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,11,123,3,12,61,192,45],"class_list":["post-11751","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-folders","tag-multimedia","tag-scripting-guy","tag-storage","tag-weekend-scripter","tag-windows-media-player-and-audio","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=11751"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11751\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=11751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}