1

I want to as the title suggests get the size in KB for each file in a directory and for this to be stored in a array along with the file name like this.

@(
('Sample.txt',10)
)

I have tried using a for each object and get item property like this

Get-ChildItem $path | ForEach-Object{
$size += Get-ItemProperty.length 
}

but it doesn't work because I don't have a variable to point it towards.

Etch-a-sketch
  • 37
  • 1
  • 6
  • Possible duplicate of [PowerShell display files size as KB, MB, or GB](https://stackoverflow.com/questions/24616806/powershell-display-files-size-as-kb-mb-or-gb) –  Dec 21 '18 at 17:04
  • `Get-ChildItem $path` already outputs objects (at least in the case of [files](https://learn.microsoft.com/dotnet/api/system.io.fileinfo)) that combine a [`Name` property](https://learn.microsoft.com/dotnet/api/system.io.fileinfo.name) with a [`Length` property](https://learn.microsoft.com/dotnet/api/system.io.fileinfo.length). Your second code snippet suggests that you are trying to compute the total size of all files in a directory, but everything before that in your question says you want the size of individual files. Can you clarify what you are trying to do? – Lance U. Matthews Dec 21 '18 at 20:20

2 Answers2

3

i think you are better served with an array of PSCustomObjects. [grin] something like the code below. the resulting collection is ready for use in other situations - such as export to a CSV file.

you may want to remove the padding on the .Size_KB property. right now it gives <some spaces><number>. if you remove the ,7 you can do away with the left padding spaces.

$SourceDir = $env:TEMP
$Filter = '*.log'

$FileList = Get-ChildItem -LiteralPath $SourceDir -Filter $Filter -File

$Results = foreach ($FL_Item in $FileList)
    {
    [PSCustomObject]@{
        Name = $FL_Item.Name
        Location = $FL_Item.Directory
        Size_KB = '{0,7:N2}' -f ($FL_Item.Length / 1KB)
        }
    }

$Results

output ...

Name                                              Location Size_KB
----                                              -------- -------
Genre-List_2018-12-18.log                         C:\Temp     0.17
Grouping-Strings-List_2018-12-19.log              C:\Temp    46.52
Grouping-Words-List_2018-12-19.log                C:\Temp     1.13
Itunes_AlbumAutoRating_Disable.ps1_2018-12-17.log C:\Temp     0.21
Itunes_Default-Rating_Set.ps1_2018-12-21.log      C:\Temp     0.21
Itunes_Genre-Cleanup.ps1_2018-12-18.log           C:\Temp     0.31
Itunes_Grouping-Cleanup.ps1_2018-12-19.log        C:\Temp     0.38
Itunes_R-PC-SC_Save.ps1_2018-12-20.log            C:\Temp     0.20
jusched.log                                       C:\Temp     9.99
vscode-inno-updater-1545349272.log                C:\Temp   337.14
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
0

What you are looking for is what in PowerShell are referred to hash tables.

When using foreach-object you can refer to the individual object in the list being iterated through using $_

The code below will accomplish what you are trying to do:

$size = @{}
get-childitem -path "C:\Temp" | foreach-object {
  $name = $_.Name
  $filesize = ((get-item $_.FullName).Length/1KB)
  $size[$name] = $filesize
}
Dan
  • 958
  • 2
  • 11
  • 25