Using Windows Powershell to find Files

Microsoft Windows Powershell

OK so I have a bunch of files on a Windows machine ( they happen to be music files).

I want to list all the files that are below a specified size. e.g. 2k

The Windows Powershell has the Get-ChildItem command (alias as ls so that it is easier on the eye for Unix/Linux types) that can do the trick. The full command is:

ls -path d:\dbp\ -R *.m4a | where-object {$_.length  -lt 2048 } | % {Write-Host `"$($_.Fullname)`" }

So lets split that up:

ls -path d:\dbp -R *.m4a

This gets me all the files with the m4a extension in all sub-folders in/below the specified root folder d:\dbp

| where-object {$_.length  -lt 2048 }

This sets my filter condition – where the files size is Less Than 2048 bytes

| % {Write-Host `"$($_.Fullname)`"}

This formats the output to give me a list containing the full path of all matching files. Note the ` character escapes the ” character so we get the full path output within quotes.

Example output:

"D:\dbp\25564-2\Various Artists\The Greatest Songs Ever; France\01 - Douce France (Charles Trénet).m4a"
"D:\dbp\25564-2\Various Artists\The Greatest Songs Ever; France\04 - Emmenez-Moi (Charles Aznavour).m4a"
"D:\dbp\25564-2\Various Artists\The Ultimate Gershwin Disc 2\09 - S'wonderful-Funny Face.m4a"
"D:\dbp\25564-2\Various Artists\The Ultimate Line Dancing Album\10 - Cotton Eye Joe (Rednex).m4a"

Now if I want to do something useful with the files… like delete them I can use the Remove-Item command. No undo – proceed with caution:

ls -path d:\dbp\ -R *.m4a | where-object {$_.length  -lt 2048 } | % { Remote-Item -literalpath $($_.Fullname) }