Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using Christian's <code>GetDirectories</code> suggestion, here's another solution that's not quite as involved:</p> <pre><code>function Get-PathCanonicalCase { param( $path ) $newPath = (Resolve-Path $path).Path $root = [System.IO.Path]::GetPathRoot( $newPath ) if ( $newPath -ne $root ) # Handle case where changing to root directory { $newPath = [System.IO.Directory]::GetDirectories( $root, $newPath.Substring( $root.Length ) )[ 0 ] } $newPath } </code></pre> <p>EDIT: Thanks for all the help.</p> <p>Btw, all I wanted this for was to use in a little utility script overriding the default cd alias, allowing me to specify some 'root' directories that are searched if the path doesn't exist relative to the current directory. I.e., it allows me to <code>cd Documents</code>, <code>cd trunk</code>, <code>cd Release-10.4</code> regardless of my current location. And it annoyed me to have the prompt in the case that I entered it, instead of its actual case.</p> <pre><code># Usage: # Set up in $profile - define the functions and reassign 'cd'. Example: # ----- # . .\Set-LocationEx.ps1 "c:\dev\Code", "c:\dev\Code\releases", "$HOME" -Verbose # if (test-path alias:cd) { remove-item alias:cd &gt; $null } # Set-Alias cd Set-LocationEx # ----- param( [parameter(Mandatory = $true)][string[]]$roots ) Set-StrictMode -Version Latest Write-Verbose "Set-LocationEx roots: $(Join-String -Strings $roots -Separator ', ')" function Set-LocationEx { param( [Parameter( Mandatory="true" )]$path ) process { $verbose = ( $PSCmdlet.MyInvocation.BoundParameters.ContainsKey( "Verbose" ) -and $PSCmdlet.MyInvocation.BoundParameters[ "Verbose" ].IsPresent ) if ( $verbose ) { Write-Verbose( "$(Join-String -Strings $roots -Separator ', ')" ) } if ( !( Test-Path $path ) ) { foreach ( $p in $roots ) { $newPath = Join-Path $p $path if ( $verbose ) { Write-Verbose "Looking for $newPath" } if ( Test-Path $newPath ) { $newPath = Get-PathCanonicalCase( $newPath ) if ( $verbose ) { Write-Verbose "Found $newPath" } Push-Location $newPath return } } } if ( Test-Path $path ) { $path = Get-PathCanonicalCase( $path ) } Push-Location $path } } function Get-LocationExRoots { process { Write-Output (Join-String -Strings $roots -NewLine) } } function Get-PathCanonicalCase { param( $path ) $newPath = (Resolve-Path $path).Path $root = [System.IO.Path]::GetPathRoot( $newPath ) if ( $newPath -ne $root ) # Handle root directory { $newPath = [System.IO.Directory]::GetDirectories( $root, $newPath.Substring( $root.Length ) )[ 0 ] } $newPath } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload