Skip to content

Instantly share code, notes, and snippets.

@sclarson
Created September 21, 2014 13:18
Show Gist options
  • Save sclarson/1a2deecc8ca4592e1ef1 to your computer and use it in GitHub Desktop.
Save sclarson/1a2deecc8ca4592e1ef1 to your computer and use it in GitHub Desktop.
Case sensitive cd/set-location in powershell 3
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
}
}
@lzybkr
Copy link

lzybkr commented Sep 22, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment