I am always looking for ways to do things faster and easier with PowerShell. One common task that I never seem to stop needing is discovering how much disk space a given folder is consuming. Even though disk space is cheap these days, I guess I'm old-school enough to want to keep things lean.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
It is simple enough to run Get-Childitem and pipe the results to Measure-Object.
But this can be often time consuming. The other drawbacks, besides typing is it takes extra work for format the results into something more user friendly. And if i want to include hidden files, I have to remember to use -Force with Get-ChildItem.
Cmdlets are nice and convenient. And I always recommend to beginner or even intermediate scripters, if there is a cmdlet to use over the .NET Framework, use the cmdlet. But there are always exceptions and as you grow in expertise, you'll realize there are times when going directly to the .NET Framework is a better choice. Which is what I have done.
In the latest version of the PSScriptTools module, I created a new function called Get-FolderSizeInfo. I still have the ease of running a command at a PowerShell prompt, but the function uses the .NET Framework to speed up enumerating and measuring files. You can view the source code here if you are interested.
Now I can easily and quickly get the size of folder. The default is to skip hidden files and foldes, but I can easily include them as well.
This also makes it easy to check top level folders.
And because the function is writing a custom object to the pipeline with a unique typename, I added a custom format ps1xml file that allows me to format results as KB, MB or GB.
As you can see from my screenshots, this command works cross platform on PowerShell 7.
If you are interested in this, you can learn more about the PSScriptTools module or install it from the PowerShell Gallery. I hope you find this as useful as I do.
Now that is a useful utility!
Thanks Jeff
Wonderful function and a superb module. Thanks a lot for sharing.