12
votes

For a simple example, let's say I have a folder, Root, with three folders in it; Folder1, Folder2, and Folder3. Each of these folders (including Root) has a bunch of files in them, including .pdb files. I want to use the PowerShell Get-ChildItem cmdlet to return all of the files in all of the folders (including Root), except for the .pdb files in Folder2. If I use:

Get-ChildItem -Path C:\Root -Recurse -Exclude *.pdb

Then I get back all of the non-.pdb files in all of the directories, which is close to what I want. So I assumed that the following would achieve what I want:

Get-ChildItem -Path C:\Root -Recurse -Exclude \*\\Folder2\\*.pdb

But this does not exclude any of the pdb files in Folder2 (or any other folders). I have tried several variants for the -Exclude filter, such as Folder2\\\*.pdb, but I cannot get it to work. In fact, even using \*\\\*.pdb does not seem to do anything; no .pdb files get excluded from any folders.

So it seems that the wildcards cannot be used for directories, only filenames, but I assume I am just doing something wrong. I found this article explaining the wildcard and range operators, but unfortunately it does not discuss using them with directory names; only file names.

5

5 Answers

11
votes

I haven't seen the exclude parameter working with directories either.

You could try piping into Where-Object. That is,

Get-ChildItem -Recurse *.pdb | Where-Object {$_.FullName -notMatch "folder2"}
5
votes
 gci "C:\Root" -Recurse | Where-Object {$_.FullName -notlike "*Folder2\*.pdb*"} | Export-CSV "C:\Root\Export.csv" -NoType

Tried, tested & one liner :-). This works as I have copied your folder & file structure to replicate. Sorry about it being a few years late, however.

Feel free to tweak the code to your needs, obviously.

4
votes

OhadH's answer is almost there.

You can use

Get-ChildItem -Recurse | Where-Object {$_.FullName -notmatch "folder2" -or $_.Name -notlike "*.pdb" } 

or

Get-ChildItem -Recurse | ? {$_.FullName -notlike "*folder2*" -or $_.Name -notlike "*.pdb" } 

It depends on where your folder2 is located. But I think you got the idea.

I hate to say it, but PowerShell is not any convenient as Bash or other shells.

0
votes

use asterisk and recurse parameter:

Get-ChildItem -Path "C:\Users\*\AppData\Roaming\Microsoft\" -Filter "*config*" -Recurse | Select -ExpandProperty FullName

-1
votes

This should be work for you:

Get-ChildItem -Path C:\Root -Exclude "*\Folder2\*.pdb" -Recurse