I
I
Ilya Vano`vich2020-11-28 13:25:27
PowerShell
Ilya Vano`vich, 2020-11-28 13:25:27

How to display the total size of a file type?

The script should output the 10 largest file types by extension, sorted by total size in descending order. It also takes 2 parameters, 1st for the search directory, 2nd for the file output.

Script:

# Присваивание имени выводимому файлу 
$args[1] += "\hard_mem_use.txt"
# Получаем список путей всех файлов, начиная с пути $args[0]
Get-ChildItem $args[0] -r|`
WHERE { -NOT $_.PSIsContainer } |`
Group Extension  -NoElement |`
# Сортировка по убыванию размера файла, разрешая использование нескольких значений 
Sort-Object -descending -property length  |`
# Фильтруем вывод информации, первые 10 имён и их вес в Мб. 
Select-Object -first 10 name, @{Name="Mb";Expression={[Math]::round($_.length / 1MB, 2)}}|`
# Вывод файла по пути $args[1]
Out-File -FilePath $args[1]

notepad.exe $args[1]


At the moment it does not show the total size:

Name     Mb
----     --
.exe      0
.zip      0
.docx     0
.log      0
.txt      0
.torrent  0
.sys      0
.rtf      0
.ini      0
.lng      0


Can you tell me where I made a mistake or maybe I forgot something?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
MaxKozlov, 2020-11-28
@vanovichivan

Because sort-object has nothing to sort. You previously grouped by extension. And outside you now have objects that have two properties - count, name. if not -noelement would still be group. And you are trying to do something with length.
you need to leave the Group property and sum the Length over it
ps back quote after the "pipe" is not needed, PS and so after it allows you to wrap the line

A
alhaos, 2020-11-29
@alhaos

param(
    $directoryName = "c:\windows",
    $outFileName = "d:\tmp\out.csv"
)

Get-ChildItem $directoryName -File | Group-Object {$_.Extension} | ForEach-Object {
    [PSCustomObject]@{
        Name = $_.Name
        Sum = ($_.Group.Length | Measure-Object -Sum).Sum
    }
} | Sort-Object -Property "Sum" -Descending | Tee-Object -Variable "list" | Export-Csv $outFileName

$list

D
Dmitry Aitkulov, 2016-10-27
@Ikbolnav

please increase CacheSize configuration parameter

here he writes what he needs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question