Saturday, April 25, 2015

#59 : Get Folder Size with Powershell

Sometimes, we need to check the folder size. Although, ls can give your results of certain files, but still you need to struggle to get the folder (directory) size. I wrote one small script to get this with Powershell. You may use this recipe if you are writing something where folder size is required to be checked.

#------------------------------------------------------------------------------------------
# Script : Get_FolderSize.ps1
# Author : Som DT.
# Purpose : Script to find the folder size
#------------------------------------------------------------------------------------------
#--Function Declaration --#
function Get-FolderSize ([string] $FolderName)
{
$value = "{0:N2}" -f ((Get-ChildItem -recurse $FolderName | Measure-Object -property length -sum).Sum / 1MB)
echo "Directory Name : ${FolderName} : Size : ${value} MB"
}
#--Calling the function--#
Get-FolderSize "C:\Temp"
#------------------------------------------------------------------------------------------


Thanks for reading this article!

No comments:

Post a Comment

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...