Question

I am trying to find all file extension in a folder and subfolders and generate a list. I found a oneliner but it do not generate the list as i want. i got mutiple of paths so i do this.

$date = get-date -Format d
$File = "C:\NoBackup\FolderPaths.txt"

foreach ($Folder in (Get-Content $File)) {
Get-ChildItem $Share -Recurse -ErrorAction SilentlyContinue | Group-Object extension | Select-Object @{Name="Folder";Expression={$Folder}}, name, @{n='TotalSize';e={$_.group | ForEach-Object -Begin {$size=0} -Process {$size += ([decimal]::round($_.Length / 1MB))} -End {"$size MB"}}} | Sort-Object -Property 'TotalSize' -Descending | Format-Table -AutoSize
}

This will give a new header foreach folder in folderpaths, and i need the result be like this

           .ext1    .ext2    .ext3    .ext4
D:\Folder1   5MB     12MB     20MB      8MB
D:\Folder2  10MB     54MB     12MB      3MB
D:\Folder3   2MB     12MB     20MB    100MB

I cant find out to rewrite the code to get what i need. Hope you can help me out with this.

Was it helpful?

Solution

The script works now. I needed to change

foreach($folder in $folders)

To

foreach($folder in (Get-Content $file))

OTHER TIPS

It is not short or sweet, but try this:

function ConvertTo-Units($decimal)
{
    $value = [decimal]::Round($decimal/1mb,2)
    $units = "MB"
    if($value -eq 0)
    {
        $value = [decimal]::Round($decimal/1kb,2)
        $units = "KB"
    }
    return "{0} {1}" -f $value,$units
}

$File = "C:\NoBackup\FolderPaths.txt"
$fileData = @{}
foreach ($folder in (Get-Content $file))
{
    $files = Get-ChildItem $folder -Recurse -ErrorAction SilentlyContinue -File
    $fileData[$folder] = $files | Select-Object extension,length | %{$h = @{}} { $h[$_.extension]  += $_.length } { $h}
}

$extensions = $fileData.Keys | % { $fileData[$_].Keys } | % tolower | Select-Object -Unique | ? { $_ }
$properties = @(
    @{Name="Folder";Expression={$_}}
)
$extensions | % {$properties += @{Name="$_";Expression=[scriptblock]::Create("ConvertTo-Units `$fileData[`$folder][""$_""]")}}
$result  = @()
foreach($folder in $folders)
{
    $result += $folder | Select-Object $properties
}
$result | ft * -AutoSize -force
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top