I am trying to find a way to return the full path to a given folder. The problem is that my code returns more than one folder if there is a similar folder with a name. for example, searching for "Program Files", returns "Program Files" and "Program Files (x86) ." Since I did not request "Program Files (x86) , I do not want it to return it. I use:
$folderName = "Program Files"
(gci C:\ -Recurse | ?{$_.Name -match [regex]::Escape($folderName)}).FullName
I was thinking of replacing -match with -eq, but it will return $ false, comparing all the way.
I thought that maybe I would return all the matches, and then ask the user to choose which one is correct, or create an array that splits the path down and executes -eq for each folder name, and then joins the path again, but mine there are no skills in the department of the array and cannot make it work.
Any help or pointers would be greatly appreciated.
thank
Here is what I used with thanks from Frode:
$path = gci -Path "$drive\" -Filter $PartialPath -Recurse -ErrorAction SilentlyContinue #| ?{$_.PSPath -match [regex]::Escape($PartialPath)}
($path.FullName | gci -Filter $filename -Recurse -ErrorAction SilentlyContinue).FullName
source
share