Created
September 24, 2017 06:41
-
-
Save bvli/4f1b8dc7d30ce33c46e9a5553a7ab9cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Implement cd - | |
# Nice inspiration from http://stackoverflow.com/questions/31888580/a-better-way-to-check-if-a-path-exists-or-not-in-powershell | |
if (Test-Path alias:cd) | |
{ | |
Remove-Item alias:cd | Out-Null | |
} | |
$_locationStack = New-Object System.Collections.Stack | |
$SetLocationCmd = Get-Command Set-Location | |
$SetLocationCmdMetadata = New-Object System.Management.Automation.CommandMetadata $SetLocationCmd | |
$Binding = [System.Management.Automation.ProxyCommand]::GetCmdletBindingAttribute($SetLocationCmdMetadata) | |
$Params = [System.Management.Automation.ProxyCommand]::GetParamBlock($SetLocationCmdMetadata) | |
$Command = { | |
try | |
{ | |
$currentDirectory = (Get-Location).Path | |
$p = $PSBoundParameters[$PSCmdlet.ParameterSetName] | |
if ( -not $p) | |
{ | |
$PSBoundParameters[$PSCmdlet.ParameterSetName] = $HOME | |
} | |
if ($p -eq "-") | |
{ | |
if ($_locationStack.Count -eq 0) | |
{ | |
$p = "." | |
} | |
else | |
{ | |
$p = $_locationStack.Pop() | |
} | |
$PSBoundParameters[$PSCmdlet.ParameterSetName] = $p | |
Set-Location @PSBoundParameters | |
} | |
else | |
{ | |
Set-Location @PSBoundParameters | |
if ($?) | |
{ | |
if ($_locationStack.Count -eq 0 -or $_locationStack.Peek() -ne $p) | |
{ | |
$_locationStack.Push($currentDirectory) | |
} | |
} | |
} | |
} | |
catch { throw $_ } | |
} | |
$Function:cd = '{0}param({1}) {2}' -f $Binding,$Params,$Command |
Uh.. I'm using it daily and have for years and I have never experienced anything like that. Neither on Windows Powershell nor Powershell core.. I know it doesn't really help you - sorry about that..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Got it, but for some reason, it doesn't work with paths that are "longer". For example, going from:
C:\Users\Dude\Downloads ---> C:\Users\Dude\Downloads\example
works, but going from
C:\Users\Dude\Downloads ---> C:\Users\Dude\Documents\Stuff\More Stuff
Doesn't throwing a
Any idea what it might be?