Frenchy
Frenchy

Reputation: 45

How to skip error of Get-ChildItem returning Access denied

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 ?

Upvotes: 3

Views: 4140

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174730

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

Upvotes: 8

Related Questions