GetDirectory.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
<#
    Module: GetDirectory.psm1
    Author: Dark-Coffee
    Version: 3.3
    Updated: 2020-10-19
    Description: dir, but it actually shows folder sizes! :O
#>


function Get-Directory {
    param (
        [Parameter(Mandatory=$false)][string]$Path = $PWD,
        [Parameter(Mandatory=$false)][string]$SizeIn = 'MB'
    )

    #Param Validation
    if('KB','MB','GB' -notcontains $SizeIn){Write-Error -Category InvalidArgument -Message 'SizeIn must be in: KB | MB | GB'}else{

    #Sort Sizes
    $SizeIn = $SizeIn.ToUpper()
    $SizeMeasure = "1$SizeIn"

    #Declare Expression
    $Size = @{n="Size ($SizeIn)";e={$_ | Get-ChildItem -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Sum | ForEach-Object {[Math]::Round(($_ / $SizeMeasure),2)}}}
    $Size2 = @{n="Size ($SizeIn)";e={$_ | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Sum | ForEach-Object {[Math]::Round(($_ / $SizeMeasure),2)}}}

    #Directory Logic
    $Directories = Get-ChildItem -Path $Path -Directory
    $DirectoryOutput = Foreach($Directory in $Directories){$Directory | Select-Object Mode, LastWriteTime, $Size, Name | Sort-Object Name}

    #Item Logic
    $Files = Get-ChildItem -Path $Path -File
    $FileOutput = Foreach($File in $Files){$File | Select-Object Mode, LastWriteTime, $Size, Name | Sort-Object Name}
    }
    
    #Build Output
    $DirectoryListing = $DirectoryOutput,$FileOutput

    #Return Output
    $DirectoryListing

    <#
    .SYNOPSIS
    Extends the Dir command to include folder sizes.

    .DESCRIPTION
    Extends the Dir command to include folder sizes.
    Will provide the standard Dir output, with a modified size column.

    .PARAMETER Path
    Specifies the path to run the command inside, else defaults to the current working directory.

    .PARAMETER SizeIn
    If specified (from KB,MB,GB) will display the folder size in that measurement.

    .INPUTS
    This module does not accept pipeline input.

    .OUTPUTS
    Standard output of Dir command, with a modified Size([unit]) column.

    .EXAMPLE
    PS> Get-Directory

    Mode LastWriteTime Size (MB) Name
    ---- ------------- --------- ----
    d---- 04/06/2020 19:44:45 35.64 Applications
    d---- 14/10/2020 20:29:38 0 Desktop
    d---- 14/10/2020 20:22:06 5181.69 Documents
    d---- 14/10/2020 20:12:51 163949.76 Downloads

    .EXAMPLE
    PS> Get-Directory -SizeIn GB

    Mode LastWriteTime Size (GB) Name
    ---- ------------- --------- ----
    d---- 04/06/2020 19:44:45 0.03 Applications
    d---- 14/10/2020 20:29:38 0 Desktop
    d---- 14/10/2020 20:22:06 5.06 Documents
    d---- 14/10/2020 20:12:51 160.11 Downloads

    .EXAMPLE
    PS> Get-Directory -Path 'E:\Steam'
 
    Mode LastWriteTime Size (MB) Name
    ---- ------------- --------- ----
    d----- 08/06/2020 18:02:51 273572.47 steamapps
    -a---- 10/09/2020 08:05:38 0.41 steam.dll

    .LINK
    https://github.com/dark-coffee/GetDirectory#readme
    #>

}

Export-ModuleMember -Function Get-Directory