Sunday, December 25, 2016

Batch-like wildcards in PowerShell

Several commands and utilities used in batch scripting take filename filters that use * and ? as wildcards for multiple characters and exactly one character, respectively. In PowerShell, that kind of matching can be done with the -like operator, but it has a couple extra features (e.g. # for a numeric character) that make it not match batch behavior exactly. Therefore, to use it on arbitrary filters and get it to work like classic batch, you should first escape some characters in the filter:

$input -like ($filter.Replace('[', '[[]').Replace('#', '[#]'))

Based on my Super User answer.

No comments:

Post a Comment