Reputation: 185
I am currently using a script I wrote to use a Logparser query against a directory recursively to produce the following output:
QTY TOT KB AVRG KB MAXM KB MINM KB
------- --------- ------- ------- -------
3173881 175101609 55 85373 0
I would like to reproduce this using powershell in an attempt to make collecting this information more portable. (Not needing to install / copy logparser)
I searched and attempted to manipulate what examples I have found but can't quite get the structure down.
Here is the closest I have gotten:
Get-ChildItem -Recurse | Measure-Object -sum Length | Select-Object Count,Average,Sum
This returns:
Count Average Sum
----- ------- ---
44663 40861708776
Any suggestions? I would prefer to stick to a "one line" command if possible.
Upvotes: 0
Views: 284
Reputation: 28963
Get-ChildItem is pretty slow; what about using RoboCopy to list the folder contents?
$Folder = "D:\Downloads"
robocopy $Folder $Folder /S /L /BYTES /NJH /NJS /NDL /V |
ForEach-Object { (-split $_)[1] } |
Measure-Object -Maximum -Minimum -Sum -Average |
Format-Table
Which goes to a one-liner:
robocopy $Folder $Folder /S /L /BYTES /NJH /NJS /NDL /V |%{(-split $_)[1]}|measure -a -s -ma -mi | ft
Robocopy options are:
/S - subdirectories (excluding empty ones)
/L - list files, don't do any copying or moving
/BYTES - show sizes in bytes, with no commas or anything
/NJH and /NJS - no header and summary lines in the output
/NDL - don't list directories
/V - verbose (do list individual files and their sizes)
Then the ForEach splits the output to drop the filename and robocopy status and just keep the size, and measure-object calculates the results, and format-table turns it into a nicer looking table output.
Upvotes: 0
Reputation: 188
Measure-Object
only does what it's told, so if you only -Sum
, you only get the Sum.
Get-ChildItem -Recurse -File | Measure-Object -Sum -Average -Maximum -Minimum -Property Length | Select Count, Average, Sum, Maximum, Minimum
Upvotes: 1