-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-FolderSize.ps1
36 lines (30 loc) · 1.22 KB
/
Get-FolderSize.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<#
.SYNOPSIS
Retrieves the size of the folder.
.DESCRIPTION
The Get-FolderSize function retrieves size of the given folder and all the subfolders within it.
.PARAMETER path
Path to the folder. This parameter is mandatory.
.EXAMPLE
Get-FolderSize C:\Example
Gets the size of the C:\Example folder.
.NOTES
For the function to work, Robocopy must be installed in one of the directories included in PATH enviromental variable. This tool is a part of standard OS installation beginning with Windows Vista and Windows Server 2008.
#>
function Get-FolderSize {
[CmdletBinding()]
param (
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,HelpMessage="Enter a path to the folder.")]
[ValidateNotNullOrEmpty()]
[string[]]$path
)
if (!(Test-Path $path)) {
throw [System.IO.FileNotFoundException] "Path $path not found."
}
if ($path.GetType().Name -eq "DirectoryInfo") {
$path = $path.FullName
}
$robocopy = robocopy $path "c:\FakeTempDir" /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh
[int]$folderSize = ($robocopy | Where-Object {$_ -match "Bytes"}).Trim().Split(":").Trim().Split(" ")[1]
Return $folderSize
}