0
votes

I have a problem with my powershell script, i'm trying to find a file in a folder recursivly. The folder where i am looking is %temp%. Unfortunatly in this folder, there is some folder protected by admin right. Then when i am using Get-ChildItem, it return nothing exept an error (UnauthorizedAccessException).

Here is my code :

$path= (Get-ChildItem -path $ENV:TEMP -force -Recurse -Include logMyApp.txt).FullName

I also tryed with -ErrorAction SilentlyContinue but it does not work.

Thank you for your time :)

Edit : Trying to say Hello, but it does not work, stackoverflow policy ?

1
Use -Filter instead of -Include and then try with -ErrorAction SilentlyContinue again :) - Mathias R. Jessen
@MathiasR.Jessen Omg you saved me many hours ! Thank you ! - Frenchy
I don't think the access denied part matters. - js2010

1 Answers

5
votes

Avoid combining the -Recurse and -Include/-Exclude parameters when using Get-ChildItem against the FileSystem provider - they are not mutually exclusive in a technical sense, but their behavior is partially redundant (-Include/-Exclude tries to recurse the file tree independently) and this can sometimes lead to unexpected, buggy and slow enumeration behavior.

For simple inclusion patterns, use -Filter in place of -Include:

$path = (Get-ChildItem -path $ENV:TEMP -force -Recurse -Filter logMyApp.txt -ErrorAction SilentlyContinue).FullName