'How to filter out filename that end in .table.sql or a two part extension?

I know how to do this with one extension in PowerShell, but since my files have a two part extension this is no longer working for me:

Get-ChildItem . -Recurse | Where-Object { $_.Extension -notin @("*.table.sql")}


Solution 1:[1]

The Extension property only contains the last . and whatever else trails after (.sql in your case).

You'll want to test the whole Name instead, and you'll want to use the -[not]like operator(s) for wildcard matching:

Get-ChildItem . -Recurse |Where-Object Name -notlike '*.table.sql'

If you want to include only *.sql files to begin with, use the -Filter parameter with Get-ChildItem:

Get-ChildItem . -Recurse -Filter '*.sql' |Where-Object Name -notlike '*.table.sql'

Solution 2:[2]

Mathias' helpful answer solves your immediate problem.

Taking a step back, you can simplify your command by passing the wildcard expression to
Get-ChildItem's -Exclude parameter:

Get-ChildItem . -Recurse -Exclude *.table.sql

Performance caveat: While this is not only conceptually simpler, it should perform better than post-filtering with a Where-Object call, but - due to an inefficient implementation - doesn't, as of PowerShell 7.2.2 - see GitHub issue #8662.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mathias R. Jessen
Solution 2 mklement0