Reputation: 1
having trouble excluding directories and all subdirectories under excluded parent. i run this but need to exclude directory and subdirectories that start with TECH or have it in path.
c:\users\user1
user2
tech-xxx (random numbers - need to exclude this and all subdirectories)
tech-xxx (random numbers - need to exclude this and all subdirectories)
These fail (along with many other variations i have tried):
$UserDirExtensions="*.cmd","*.bat"
gci C:\Users -Recurse -Include $UserDirExtensions | Where-Object {!($_.FullName -icontains 'tech*')
$Folders = (gci C:\Users -Directory | Select-Object -Property name | Where {$_.Name -notlike 'tech*' })
foreach ($i in $Folders) {
gci C:\Users\$i -Recurse -Include $UserDirExtensions
}
thanks in advance.
Upvotes: 0
Views: 449
Reputation: 27516
name isn't fullname. -contains doesn't do what you think, it matches whole words in an array basically, and doesn't use wildcards.
dir -r | where fullname -notlike *tech*
There's a couple ways to focus on the parent, but this wouldn't exclude a tech folder itself:
dir -r | where psparentpath -notlike *tech*
dir -r | where directory -notlike *tech*
dir -r | where directoryname -notlike *tech*
Upvotes: 3