
I built the body from strings in memory - no point writing it to a file, just to reread it. Then I sorted by size, and exported the result into a csv file for attachment. When selecting the size, I built custom objects containing the information (Name & size), since if we want to sort by size, we need to keep it as a separate property. I used the Scripting.FileSystemObject Comobject to measure size, since that's a bit faster. \TEST.csv -Encoding (::UTF8) -Body $body -SmtpServer Īs you can see, some slight modifications :) Send-MailMessage -From -To -Subject "Test Theme" -attachment. $colItems = Get-ChildItem $startFolder | Where-Object Click the Size drop-down menu and choose the file size range youd like to search. Click the Size column to sort by largest to smallest. Type 'size: ' into the search box (the one over top right) and in the drop down box, select Gigantic. I've done the honors: $startFolder = "C:\VMs" Open Windows Explorer by clicking the folder icon on your taskbar. Select 'This PC' in the left pane to search the computer. As a result, such files can be counted several times.You can do this, but it'll require some reformatting your script. For example, the C:\Windows folder contains many hard links to files in the WinSxS folder (Windows Component Store). This script incorrectly calculates the size of a directory if it contains symbolic or hard links.
In order to prevent directory access errors (PermissionDenied -> DirUnauthorizedAccessError), use the -ErrorAction Silentl圜ontinue parameter.