Powershell Get-ChildItem -recurse doesn't get all items

Daniel Olsen picture Daniel Olsen · May 29, 2012 · Viewed 21.3k times · Source

I'm working on a powershell script erase certain files from a folder, and move the rest into predefined subfolders.

My structure looks like this

Main
    (Contains a bunch of pdb and dll files)
    -- _publish
        --Website
            (Contains a web.config, two other .config files and a global.asax file)
            -- bin
                (Contains a pdb and dll file)
            -- JS
            -- Pages
            -- Resources

I want to remove all pdb, config and asax files from the entire file structure before I start moving them. To which I use:

$pdbfiles = Get-ChildItem "$executingScriptDirectory\*.pdb" -recurse

foreach ($file in $pdbfiles) {
    Remove-Item $file
}

And so on for all filetypes I need removed. It works great except for a pdb file located in the bin folder of the website. And for the ASAX file in the website folder. For some reason they get ignored by the Get-ChildItem recurse search.

Is this caused by the depth of the items within the resursive structure? Or is it something else? How can I fix it, so it removes ALL files as specified.

EDIT: I have tried adding -force - But it changed nothing

ANSWER: The following worked:

$include = @("*.asax","*.pdb","*.config")
$removefiles = Get-ChildItem "$executingScriptDirectory\*" -recurse -force -include $include 

foreach ($file in $removefiles) {
    if ($file.Name -ne "Web.config") {
        Remove-Item $file
    }
}

Answer

David Brabant picture David Brabant · May 29, 2012
Get-ChildItem -path <yourpath> -recurse -Include *.pdb