LibraryDirectory.ps1 1.65 KB
Newer Older
O'Reilly Media, Inc.'s avatar
O'Reilly Media, Inc. committed
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
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)

Set-StrictMode -Version Latest

## Get the size of all the items in the current directory
function Get-DirectorySize
{
    <#

    .EXAMPLE

    PS > $DebugPreference = "Continue"
    PS > Get-DirectorySize
    DEBUG: Current Directory: D:\lee\OReilly\Scripts\Programs
    Directory size: 46,581 bytes
    PS > $DebugPreference = "SilentlyContinue"
    PS > $VerbosePreference = "Continue"
    PS > Get-DirectorySize
    VERBOSE: Getting size
    VERBOSE: Got size: 46581
    Directory size: 46,581 bytes
    PS > $VerbosePreference = "SilentlyContinue"

    #>

    Write-Debug "Current Directory: $(Get-Location)"

    Write-Verbose "Getting size"
    $size = (Get-ChildItem | Measure-Object -Sum Length).Sum
    Write-Verbose "Got size: $size"

    Write-Host ("Directory size: {0:N0} bytes" -f $size)
}

## Get the list of items in a directory, sorted by length
function Get-ChildItemSortedByLength($path = (Get-Location), [switch] $Problematic)
{
    <#
    
    .EXAMPLE

    PS > Get-ChildItemSortedByLength -Problematic
    out-lineoutput : Object of type "Microsoft.PowerShell.Commands.Internal.Fo
    rmat.FormatEntryData" is not legal or not in the correct sequence. This is
    likely caused by a user-specified "format-*" command which is conflicting
    with the default formatting.

    #>

    if($Problematic)
    {
        ## Problematic version
        Get-ChildItem $path | Format-Table | Sort Length
    }
    else
    {
        ## Fixed version
        Get-ChildItem $path | Sort Length | Format-Table
    }
}