PowerShell: Find the exact folder name

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
+4
source share
1 answer

-matchsimilar to request *Program Files*. You should use a parameter -Filter Get-ChildItemfor something like this. It is much faster and does not require regular expressions, etc.

PowerShell 3:

$folderName = "Program Files"
(gci -path C:\ -filter $foldername -Recurse).FullName

PowerShell 2:

$folderName = "Program Files"
gci -path C:\ -filter $foldername -Recurse | Select-Object -Expand FullName

, -Recurse, ( ).

+7

Source: https://habr.com/ru/post/1529645/


All Articles