14

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.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
deadlydog
  • 22,611
  • 14
  • 112
  • 118

5 Answers5

12

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"}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paul Rowland
  • 8,244
  • 12
  • 55
  • 76
  • No, this isn't what I'm looking for. It would return ONLY the .pdb files that aren't in Folder2, but I want ALL of the files, except for the .pdb files that are in Folder2. But you may be on path for a possible solution by piping into Where-Object; although I would still like to know how to use the wildcard character with directories (or if it's just not possible). – deadlydog Jan 24 '13 at 17:56
6

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CS Pei
  • 10,869
  • 1
  • 27
  • 46
5
 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.

unkn0wn
  • 59
  • 2
  • 6
1

Use asterisk and recursion parameter:

Get-ChildItem -Path "C:\Users\*\AppData\Roaming\Microsoft\"
              -Filter "*config*" -Recurse | Select -ExpandProperty FullName
Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
-1

This should be work for you:

Get-ChildItem -Path C:\Root -Exclude "*\Folder2\*.pdb" -Recurse
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
OhadH
  • 156
  • 1
  • 9