Powershell CDPATH functionality?

Has anyone implemented the equivalent behavior of bash 'cdpath' in Powershell?

+5
source share
1 answer

I did not know about CDPATH before. Good to know. I hacked below for Powershell:

function cd2 {
    param($path)
    if(-not $path){return;}

    if((test-path $path) -or (-not $env:CDPATH)){
        Set-Location $path
        return
    }
    $cdpath = $env:CDPATH.split(";") | % { $ExecutionContext.InvokeCommand.ExpandString($_) }
    $npath = ""
    foreach($p in $cdpath){
        $tpath = join-path $p $path
        if(test-path $tpath){$npath = $tpath; break;}
    }
    if($npath){
        #write-host -fore yellow "Using CDPATH"
        Set-Location $npath
        return
    }

    set-location $path

}

It will not be perfect, but it works on hold. You can extend it, I think. Add it to your profile. If necessary, also add an alias:

set-alias -Name cd -value cd2 -Option AllScope
+6
source

All Articles