How do I get a path with the correct (canonical) case in PowerShell?

后端 未结 4 592
长情又很酷
长情又很酷 2021-01-18 11:34

I have a script that accepts a directory as an argument from the user. I\'d like to display the name of the directory path as it is displayed in Windows. I.e.,

<         


        
4条回答
  •  -上瘾入骨i
    2021-01-18 12:04

    I found a different and simpler approach using PowerShell wild cards.

     $canonicalCasePath = Get-ChildItem -Path $wrongCasingPath.Replace("\","\*") | Where FullName -IEQ $wrongCasingPath | Select -ExpandProperty FullName
    
    • The first part of the pipe replaces all backslashes in the path by backslash and asterisk \\* and return all matching files
    • The where part makes sure that only the desired file is returned and not any other potential match. IEQ is case insesitive equal
    • The last select part extracts canonical case path of the file

提交回复
热议问题