Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PowerShell support negation syntax in glob pattern?

In Linux, Bash supports negation syntax in glob pattern like [!abc] for one character other than a, b, and c.

I'm trying to find something similar in PowerShell.

In Supporting Wildcard Characters in Cmdlet Parameters, there is no description related to negation. Also, when test.txt exists, I confirmed the following, which indicates that ! is not supported as negation syntax like Bash.

  • ls tes[!s].txt returns nothing.
  • ls tes[!t].txt returns test.txt

Does PowerShell support negation syntax in glob pattern? If not, could you share some alternatives?

like image 525
Tomoyuki Aota Avatar asked Mar 08 '23 10:03

Tomoyuki Aota


1 Answers

No, it does not (as you correctly deduced from such a feature not being documented, nor working).

An alternative are regular expressions, but you'd have to do the filtering yourself, then:

Get-ChildItem | Where-Object Name -match '^tes[^t]\.txt$'
like image 188
Joey Avatar answered Mar 15 '23 16:03

Joey