Hans Hubert Vogts
Hans Hubert Vogts

Reputation: 133

Get-ChildItem with multiple paths - Error if directory is missing

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?

Upvotes: 3

Views: 1179

Answers (1)

AdamL
AdamL

Reputation: 13191

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.

Upvotes: 4

Related Questions