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 |
I source it from my profile by adding this line to it:
. ./Set-LocationEx.ps1
If you just want to try it out, you can just do the same from the command line prompt.
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
cd: Can't find acces route 'C:\Users\Dude\Documents\Stuff\More Stuff\- because it doesn't exist+
+ cd -
+ ~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\Dude\D...shell scripts\-:String) [Set-Location], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand'
Any idea what it might be?
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
I know it's been a while, but how is this exactly used? This is a really useful script, and I'd like to know using it so it works as an "cd -", as mentioned in your comment here: https://www.hanselman.com/blog/SpendLessTimeCDingAroundDirectoriesWithThePowerShellZShortcut.aspx