Add-RelativePathCapture.ps1 1.46 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
##############################################################################
##
## Add-RelativePathCapture
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Adds a new Out-Default command wrapper that captures relative path
navigation without having to explicitly call 'Set-Location'

.EXAMPLE

PS C:\Users\Lee\Documents>..
PS C:\Users\Lee>...
PS C:\>

.NOTES

This commands builds on New-CommandWrapper, also included in the Windows
PowerShell Cookbook.

#>

Set-StrictMode -Version Latest

New-CommandWrapper Out-Default `
    -Process {
        if(($_ -is [System.Management.Automation.ErrorRecord]) -and
            ($_.FullyQualifiedErrorId -eq "CommandNotFoundException"))
        {
            ## Intercept all CommandNotFound exceptions, where the actual
            ## command consisted solely of dots.
            $command = $_.TargetObject
            if($command -match '^(\.)+$')
            {
                ## Count the number of dots, and go that many levels (minus
                ## one) up the directory hierarchy.
                $newLocation = "..\" * ($command.Length - 1)
                if($newLocation) { Set-Location $newLocation }

                ## Handle the error
                $error.RemoveAt(0)
                $_ = $null
            }
        }
    }