8

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
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Daniel Olsen
  • 1,020
  • 2
  • 15
  • 27
  • Have you tried with `Get-ChildItem -force`? – CB. May 29 '12 at 11:35
  • 2
    I know I'm late to the party. The script above works in PowerShell v3 and up. I ran into this problem when I found out that the production server only had PowerShell v2. You can check by dumping the `$PSVersionTable` variable – JamesQMurphy Oct 26 '15 at 20:43

2 Answers2

20
Get-ChildItem -path <yourpath> -recurse -Include *.pdb
David Brabant
  • 41,623
  • 16
  • 83
  • 111
2

You can also use the pipe for remove:

Get-ChildItem -path <yourpath> -recurse -Include *.pdb | rm
odrix
  • 21
  • 2