4

Get Directory size in GB using PowerShell

 3 years ago
source link: https://www.dotnetforall.com/get-directory-size-in-gb-using-powershell/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Apr 15 2021

Get Directory size in GB using PowerShell

Hello friends, In this small article I will help you to get the size of any windows directory using a simple PowerShell command. With the help of the code snippet you will understand how to create and use functions in PowerShell, How to pass parameters and how to call .NET library.

Simple method to get get directory size in GB

function GetDirInfo($dir){
    $results  =  Get-ChildItem $dir -Recurse | Measure-Object -Property Length -Sum
    return [math]::round(($results).Sum/ 1GB, 3);
}

I have defined a simple method in the above code snippet. It takes the directory as parameter. $dir is the variable.

Variables in power shell are defined prefixing $ sign.

Get-ChildItem helps to get all the sub directories and objects in the main directory. Measure-Object is an in built method which returns the size of the object.

math is a .NET library which provides the round method. It in turns help to get the size in rounded to 3 nearest digits.

Below is the image snapshot which helps to call the method we have define and it helps to Get Directory size in GB using PowerShell as some numeric value.

My Learning Resource

Excel your system design interview

Like this:

Related

Written by Vikram Chaudhary · Categorized: Uncategorized · Tagged: PowerShell


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK