Answer the question
In order to leave comments, you need to log in
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]
Name Mb
---- --
.exe 0
.zip 0
.docx 0
.log 0
.txt 0
.torrent 0
.sys 0
.rtf 0
.ini 0
.lng 0
Answer the question
In order to leave comments, you need to log in
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
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
please increase CacheSize configuration parameter
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question