Sometimes you may want to search a filename in a particular location and get the file path. You can use Get-ChildItem cmdlet with -Recurse and -Filter options to recursively search a file name in a particular directory location. Here is an example for your reference –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$Location = "C:\Windows\System32" $FileName = "hosts" Get-ChildItem ` -Path $Location ` -Filter $FileName ` -Recurse ` -ErrorAction SilentlyContinue ` -Force | Select-Object DirectoryName, FullName ## Returns ## DirectoryName FullName ## ------------- -------- ## C:\Windows\System32\drivers\etc C:\Windows\System32\drivers\etc\hosts |