Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell get case sensitive path as displayed in windows

I'm looking for a way to get the my local path that includes any camel cases that are used in the names. The primary reason is so I can use the same path to make a call in windows wsl. For example, in windows I can call a file as

c:\FoO\bar.txt 
c:\Foo\Bar.txt

and windows will display it as c:\foo\bar.txt. When I try to enter the WSL with bash I need to know the actual path since Linux is case sensitive.

I have tried using

$PSScriptRoot
Split-path C:\FoO\Bar.txt
(get-Item c:\Foo\Bar.txt).FullName

but they will only provide the path that to call the script.
How do I get my path as it's displayed in the windows os? I can't just call the full path of the file I need since I can't guarantee the root directory it starting from. I also don't want to burn up cycles doing a find.

like image 757
Eric Avatar asked Aug 31 '25 03:08

Eric


2 Answers

What you want is to look at the Target property you get back from Get-Item. Fullname will come back however you typed it initially, but Target is actually a code property that seems to get the raw path of the object.

(get-Item c:\Foo\Bar.txt).Target
like image 105
Sambardo Avatar answered Sep 02 '25 15:09

Sambardo


from windows 10 onwards, the Target property is empty.
(Get-Item '....') doesn't contain any property that holds the case sensitive name.

however, when getting all child items of a folder, you get all the names case sensitive.

so my solution is like this:

$myCaseInsensitiveFileName = 'c:\FoO\bar.txt'

$allFiles = Get-ChildItem 'c:\' -Recurse
$caseSensitiveName = $allFiles.FullName | Where-Object { $_.FullName -eq $myCaseInsensitiveFileName }

$caseSensitiveName holds C:\Foo\Bar.txt.

note:

  • this can be optimized, but you get the idea.
  • the -eq operator compares strings case insensitive
like image 41
Floris Devreese Avatar answered Sep 02 '25 16:09

Floris Devreese