Aditya
Aditya

Reputation: 100

Powershell script to get csv file with file name and file size for a folder

I have folder and there are sub folders in it. I need file name and size in the csv for all the files in the folder.

Upvotes: 3

Views: 8518

Answers (1)

sahus
sahus

Reputation: 378

This code should work for you

$Directory = "C:\path to directory"
Get-ChildItem -Path $Directory -Recurse -Force | ForEach {
    [PSCustomObject]@{
        Name = $_.Name
        Size = "$([int]($_.length / 1mb)) MB"
    }
} | Export-Csv -Path "./temp.csv" -NoTypeInformation

Upvotes: 6

Related Questions