1
votes

I am trying to recursively get the files from a folder with multiple sub-folders. I noticed that Windows XP created this 5b....cb\amd64 files in the folder and it keeps throwing me the access denied error.

Get-ChildItem: Access to path is denied.

I do not have admin rights in these machines and what I am trying to do is skip these folders.

Is there a way to do this? Here is what I have tried with no success. Is it a good idea to suppress these messages as it does not break the script? Any help will be appreciated.

Get-ChildItem $sourceDir -Recurse  | ? { $_.FullName -notlike '*\5b...cb\*'} 
| Where-Object {$_.CreationTime.Date -eq ((Get-Date).Date)} | 
foreach {
    Write-Host $_
}
1
Get-ChildItem $sourceDir -Recurse -ErrorAction SilentlyContinue?Mathias R. Jessen
The problem you are going to run into is that the -exclude only works at the leaf level, so it's not useful in excluding a directory's sub-items when using -recurse. The access denied errors is a natural filter, and Mathias's suggestion that you just ignore the errors is a good idea.BenH
that sounds good. Thank you for your help.NepCoder
@MathiasR.Jessen I tried it with the ErrorAction flag and it looks good. Can you copy your command and submit it as a answer so I can accept it for future references.NepCoder

1 Answers

4
votes

The error you see is thrown by Get-ChildItem, so trying to filter the inaccessible files further down the pipeline won't help. You need to suppress the original error using the ErrorAction common parameter:

Get-ChildItem $sourceDir -Recurse -ErrorAction SilentlyContinue | ...