Created
September 21, 2014 13:18
-
-
Save sclarson/1a2deecc8ca4592e1ef1 to your computer and use it in GitHub Desktop.
Case sensitive cd/set-location in powershell 3
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
function GetCaseSensitivePath{ | |
param($pathName) | |
$pathExists = Test-Path($pathName) | |
if (-Not $pathExists){ | |
return $pathName | |
} | |
$directoryInfo = New-Object IO.DirectoryInfo($pathName) | |
if ($directoryInfo.Parent -ne $null){ | |
$parentPath = GetCaseSensitivePath($directoryInfo.Parent.FullName) | |
$childPath = $directoryInfo.Parent.GetFileSystemInfos($directoryInfo.Name)[0].Name | |
return(Join-Path $parentPath $childpath -resolv) | |
}else{ | |
return $directoryInfo.Name | |
} | |
} | |
$executionContext.SessionState.InvokeCommand.PostCommandLookupAction = { | |
param($CommandName, $CommandLookupEventArgs) | |
#Only hook cd | |
if($CommandLookupEventArgs.CommandOrigin -eq "Runspace" -and $CommandName -eq "cd"){ | |
$CommandLookupEventArgs.CommandScriptBlock = { | |
$x = Resolve-Path($args) | |
$x = GetCaseSensitivePath($x) | |
Set-Location $x | |
} | |
$CommandLookupEventArgs.StopSearch = $true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are a few problems with this.
Line 26 - this assume cd is aliased to Set-Location. If you use PowerShell community extensions, cd is aliased to Set-LocationEx.
Line 24 and 25 - mostly a nit, but you shouldn't use parens. It's harmless with one argument, but it confuses people if you use parens because they think they need parens and commas for multiple arguments but you use spaces to separate arguments to PowerShell functions.