question

NSimonKaz-2619 avatar image
0 Votes"
NSimonKaz-2619 asked IanXue-MSFT answered

Append file sizes to a file

I need some help modifying a PowerShell script that gives the sizes of folders and outputs the information to a txt file. My output file currently has headers for each row of data that is appended to the file and I want to make it look more like a list with the headers only appearing once at the top. Here is the code:

$Folder = "D:\Media\Movies\G"; gci -force $Folder -recurse | Measure-Object Length -Sum | Select @{Name="Folder";Expression={$Folder}},@{Name="Size";Expression={$_.Sum}} | Out-File -Append -FilePath .\Size.txt

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

You can add all the folders to $Folder so the script will only run Out-File once.

 $Folders = @("D:\Media\Movies\G","D:\Media\Movies\H","D:\Media\Movies\I")
 $Folders | foreach-object{
     $Folder = $_
     gci -force $Folder -recurse | Measure-Object Length -Sum | Select @{Name="Folder";Expression={$Folder}},@{Name="Size";Expression={$_.Sum}}
 } | Out-File -FilePath .\Size.txt

Or you may try to export to a CSV file.

 gci -force $Folder -recurse | Measure-Object Length -Sum | Select @{Name="Folder";Expression={$Folder}},@{Name="Size";Expression={$_.Sum}} | 
 Export-Csv -NoTypeInformation -Append -Path .\Size.csv

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.