r/PowerShell • u/SkullyRed • Jan 12 '26
Google solutions
Google search: find all files under 5mb mp3
This is what a Google search produced as a powershell command/script:
$( $Files = Get-ChildItem -Path "\\PC\Users\name\Music" -Recurse -Filter *.mp3 | Where-Object {$_.Length -lt 5MB} | Sort-Object Length) $Files | ForEach-Object { "$($_.FullName) - $(\'{0:N2}\' -f ($_.Length/1Kb))Kb" } >>C:\tmp\output.txt
The result:
At C:\Users\mike\Desktop\PowerShell\MP3 Under 5MB.ps1:1 char:143
+ ... Where-Object {$_.Length -lt 5MB} | Sort-Object Length) $Files | ForEa ...
+ ~~~~~~
Unexpected token '$Files' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
My powershell prowess score from 1-10 is (.05+/-).
I Ctrl-C, Ctrl-V, modify, save, run. In other words, I'm no programmer or powershell expert.
Why does this not work?
4
u/SVD_NL Jan 12 '26
Those are two seperate commands, not a single command:
$( $Files = Get-ChildItem -Path "\\PC\Users\name\Music" -Recurse -Filter *.mp3 | Where-Object {$_.Length -lt 5MB} | Sort-Object Length)$Files | ForEach-Object { "$($_.FullName) - $(\'{0:N2}\' -f ($_.Length/1Kb))Kb" } >>C:\tmp\output.txtI personally don't like this solution too much, but it should work.
2
u/CeleryMan20 Jan 12 '26
Eeew.
Option 1. Remove the second “$Files”.
Option 2. Split it into two separate command lines, one with “$Files = …” and the next with “$Files | …” (and get rid of the $(…) around the first pipeline).
Option 3. Remove both $Files completely and pipe Sort-Object straight to Where-Object (and get rid of the $(…) around the first pipeline).
Also consider: do you care if the results are sorted? Do you really want the output in “Filename nnnnnKb” text format, or would a CSV be handier?
ETA: is your Users folder really shared as \PC\Users ?
1
u/TILYoureANoob Jan 12 '26
Just remove the
$Files =. It looks like Gemini tried combining two sources into one script, but did it incorrectly.1
u/psdarwin Jan 12 '26
AI question - did you try feeding the error message back into the AI? I often find that it will probably say something like "You're absolutely right! I got that wrong. Here's the corrected script"
I fed the original script into copilot and asked it to make it better (plus give improved output). I didn't test it, but it at least smells like better PowerShell. And it outputs to a CSV file, which is far more usable than a flat text output.
```
2
u/bjornwahman Jan 12 '26
Define $Files first, then pipe
2
u/Godcry55 Jan 13 '26
Claude 4.5 is best if you don’t want to write code yourself.