Take Smaller File Path

Take Smaller File Path

I want to take path of one of two identical file in a folder. I'm using:

Get-ChildItem C:\Folder\SomeSubFolder\AnotherFolder\LastFolder\ -recurse -Filter *Someexefiles.exe

How can I modify this code?

1 Answer

The below will give you the name & full path for the shortest name found from the results of your filter.

Get-ChildItem C:\Folder\SomeSubFolder\AnotherFolder\LastFolder\ -recurse -Filter *Someexefiles.exe | Select Name, FullName, @{Name = "NameLength"; Expression = {$_.Name.Length}} | Sort NameLength | Select Name,FullName -First 1

If it is the full path that you need to be the shortest its should just be a case of replacing Expression = {$_.Name.Length} with Expression = {$_.FullName.Length}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.