Get-FolderSize.psm1

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
function Get-FolderSize
{
param(
    #Path of Folder to check
    [Parameter(Mandatory=$true,
    ValueFromPipeline=$true)]
    [string]$Path,
    #the depth to check recursively
    [Parameter(Mandatory=$false,
    ValueFromPipeline=$false)]
    [int]$Depth = 1,
    #show at out-gridview then return object
    [Parameter(Mandatory=$false,
    ValueFromPipeline=$false)]
    [Switch]$OutGridView,
    #a list of folder to exclude
    [Parameter(Mandatory=$false,
    ValueFromPipeline=$false)]
    [System.Array]$ExcludeList,
    #Return much property (Name,FullName,GB,Mode,Parent,CreationTime,LastAccessTime,LastWriteTime,Attributes) then (FullName,GB)
    [Parameter(Mandatory=$false,
    ValueFromPipeline=$false)]
    [Switch]$AllProperty

)
    $array = @()
    $i=0

    Write-Host "Retriving Folder in $path operation in progress ..." -ForegroundColor Cyan
    if($AllProperty -eq $false)
    {
    $listdir = Get-ChildItem -Directory -Path $path -Recurse -Depth $Depth -ErrorAction SilentlyContinue | select FullName | % {$_ -replace("@{FullName=","") -replace("}","")}
    }
    else
    {
    $list_property = Get-ChildItem -Directory -Path $path -Recurse -Depth $Depth -ErrorAction SilentlyContinue | select Name,FullName,Mode,Parent,CreationTime,LastAccessTime,LastWriteTime,Attributes
    $listdir = $list_property.FullName
    }
    $im = 0
    foreach($dir in $listdir)
    {
        
        if($ExcludeList.Length -gt 0)
        {
        $isexclude = $false
        foreach($exclude in $ExcludeList)
        {
            if($dir -like "*$exclude*")
            {
            $isexclude = $true
            }
        }
        if($isexclude)
        {
            Write-Host "$dir was exclude of calculating" -ForegroundColor Yellow
        }
        else
        {
            $tot = $listdir.Count
            $i++
            $per = [math]::Round(($i*100)/$tot)
            Write-Progress -Activity "Calculating Folder Size" -Status "$per`% Calculating: $dir" -PercentComplete $per
            $size = Get-ChildItem -Path $dir -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -sum -ErrorAction SilentlyContinue | select Sum | % {$_ -replace("@{Sum=","") -replace("}","")}
            [int]$gb = [math]::Round($size / 1GB)

            if($AllProperty)
            {
                $array += [pscustomobject]@{Path="$dir";Gb=$gb;Name=$list_property.Name[$im];Mode=$list_property.Mode[$im];Parent=$list_property.Parent[$im];CreationTime=$list_property.CreationTime[$im];LastAccessTime=$list_property.LastAccessTime[$im];LastWriteTime=$list_property.LastWriteTime[$im];Attributes=$list_property.Attributes[$im]}
            }
            else
            {
                $array += [pscustomobject]@{Path="$dir";Gb=$gb}
            }
        }
        }
        else
        {
            $tot = $listdir.Count
            $i++
            $per = [math]::Round(($i*100)/$tot)
            Write-Progress -Activity "Calculating Folder Size" -Status "$per`% Calculating: $dir" -PercentComplete $per
            $size = Get-ChildItem -Path $dir -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -sum -ErrorAction SilentlyContinue | select Sum | % {$_ -replace("@{Sum=","") -replace("}","")}
            [int]$gb = [math]::Round($size / 1GB)

            if($AllProperty)
            {
                $array += [pscustomobject]@{Path="$dir";Gb=$gb;Name=$list_property.Name[$im];Mode=$list_property.Mode[$im];Parent=$list_property.Parent[$im];CreationTime=$list_property.CreationTime[$im];LastAccessTime=$list_property.LastAccessTime[$im];LastWriteTime=$list_property.LastWriteTime[$im];Attributes=$list_property.Attributes[$im]}
            }
            else
            {
                $array += [pscustomobject]@{Path="$dir";Gb=$gb}
            }
        }
        $im++
     }
        
    Write-Progress -Activity "Calculating Folder Size" -Completed
    if($OutGridView)
    {
    $array | Out-GridView -Title "Size of all SubFolder in $Path ($Depth of depth)"
    }
    else
    {
    return $array
    }
}

Export-ModuleMember -Function Get-FolderSize