3

I am using Get-ChildItem to collect files in multiple paths.

For example

Get-ChildItem -Path c:\Test1, c:\Test2 -Filter *.xmd -Recurse -File 

If one of the directories is missing I get errors like these

Get-ChildItem : Access to the path 'C:\windows\temp" is denied.

First, I do not understand why this directory is searched and second what is the way to avoid this?

1 Answer 1

4

I'm guessing that one of those paths does not exists. Get-ChildItem has a few counter-intuitive behaviours. It may be a mix of interpreting the input, legacy functionality and maybe a bug or two.

If you don't use -Recurse, you'll get Cannot find path ... error as expected. It will also work properly if you add backslashes to paths:

Get-ChildItem -Path c:\Test1\, c:\Test2\ -Filter *.xmd -Recurse -File

or use -LiteralPath (-Path accepts wildcards):

Get-ChildItem -LiteralPath c:\Test1, c:\Test2 -Filter *.xmd -Recurse -File

Add -ErrorAction Continue or -ErrorAction SilentlyContinue if you don't want execution to stop at missing path error.

Sign up to request clarification or add additional context in comments.

1 Comment

I never thought of adding backslash at the end makes a difference. But it does the job. -ErrorAction was my first try, but it takes longer to get the files, cause more directories are scanned.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.