PoshCode Archive  Artifact [c842a6eeeb]

Artifact c842a6eeeb529c91865f00b877e288dc242e0e1718ad9e9f009825d93047c08b:

  • File LibraryDirectory.ps1 — part of check-in [fcdf4a3a32] at 2018-06-10 13:06:45 on branch trunk — From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes (user: Lee Holmes size: 2039)

# encoding: ascii
# api: powershell
# title: LibraryDirectory.ps1
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 0.1
# type: function
# author: Lee Holmes
# license: CC0
# function: Get-DirectorySize
# x-poshcode-id: 2190
# x-archived: 2016-03-18T21:12:13
# x-published: 2011-09-09T21:41:00
#
#
## 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
    }
}