
#Adding a trailing slash at the end of $path to make it consistent. ParameterSetName = "ShowTopFolderAllItemsAndAllFolders" If you have any questions feel free to drop me a comment and I’ll do my best to get back to you. There are 3 decimal places so smaller files won’t show 0 size.Get-DirectoryTreeSize -Path C:\Temp -Recurse | Sort-Object FolderSizeInMB -Descending will quickly get the largest folder in your query.AllItemsAndAllFolders will get all files, all folders and the total size for the specified directory and all subdirectories.

To avoid long subfolder strings, subfolders will display “.\” instead of $Path.Recurse shows files, folders and sizes for each directory respectively.Only specifying the path parameter will show files, folders and size for the specified directory.


The goal of the script was to have Powershell get all files in a directory and subdirectories with size. I wanted something a bit more portable and most of all, I wanted something clean! I decided I was going to write a Powershell script to get folder sizes on remote computers and ultimately came up with Get Directory Tree Size Using Powershell. I know there are programs such as windirstat and treesize but I didn’t necessarily want to install anything on my server, much less worry about patching it or removing it later. I thought it to be highly unusual that it could be filled up so fast so I wanted to see what folders were taking the most space remotely. The problem was that this was a relatively new server with several terabytes of disk space. Typically, I would just go into VMware vCenter, expand the disk and call it a day. The other day I got some alerts saying that one of our file servers was running out of space.
