Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to get child items of a folder recursively. However the folder contains noise files and folder (actually, this is a Visual Studio project folder).

Here what I have:

$root = Get-Item C:\Projects\MyProject

$allItems =  Get-ChildItem $root -Recurse -exclude "**\pkgobj\*"

However, $allItems still contains files and folder matching the paths.

What I did wrong?

To be more precise, I want to get both folders and files, but not the specified folder, and any of its descendant.

I also tried:

foreach($item in $allItems){
    if($item.FullName -notmatch "pkgobj") {
        Write-Host -ForegroundColor Green $item.FullName.Replace($root,'')


But no more success

share|improve this question

1 Answer 1

up vote 6 down vote accepted

Took me a moment to understand the issue. Its a tricky one.
-exclude only applies to the name of an item, not the fullname which you want.
So you can't use -exclude.

But here is a workaround using -notlike and Fullname

$root = "C:\Projects\MyProject"
$allitems = GCI $root -recurse | Where {$_.FullName -notlike "*\pkgobj*"} 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.