One Inch of Power: Bulk file relocation

Published September 30, 2014 by FoxDeploy

Hi guys,

Back for another installment of One inch of Power. In this episode, I became sick of seeing my music folder full of folders for albums, each of which may only have a single file or two.

I wrote this one incher to grab all folders with less than three files and move them all into a single folder, for ease of sorting.

"$HOME\Music" | gci |
   ? PSIsContainer -eq $true | % {
       if ((gci $__.FullName -recurse).Count -le 3){
          gci $__ -recurse *.mp3 | move-item -Destination $HOME\\Music\\misc 

Here’s the overall flow.

  1. Get Items from $HOME\Music
  2. Filter down to only ones where PSIsContainer equals True, which returns only folders
  3. If the number of items in the folder is less than or equal to three…
  4. …get a list of all files in the directory
  5. Move all of those items into $Home\Music\Misc

Pretty amazing what one line of PowerShell can do.

Microsoft MVP

Five time Microsoft MVP, and now I work for the mothership

Need Help?

Get help much faster on our new dedicated Subreddit!

depicts a crowd of people in a night club with colored lights and says 'join the foxdeploy subrreddit today'

Blog Series
series_sml_PowerShellGUI series_sml_IntroToRaspberryPi Programming series_sml_IntroToWindows Remote Management Series The Logo for System Center Configuration Manager is displayed here Depicts a road sign saying 'Learning PowerShell Autocomplete'

Blog Stats