Search code examples
powershellpowershell-4.0

Filtering with wildcards


How can I use wildcards to filter a result to be like this using Get-ChildItem | Where-Object?

ApplicationName 20XX - English

Whatever wildcard or operator (-match, -like, -eq) I use, it returns nothing, or something more than I want.

Basically, I just want the numbers to be 'wildcarded' to search for 2017-2020, and the rest of the string to be static. Is that possible?

My thought was something like, but unfortunally I cannot get it to work:

Where-Object DisplayName -eq 'Application 20[17-20] - English'   
Where-Object DisplayName -Match 'Application 20%% - English'   
Where-Object DisplayName -eq 'Application 20* - English' 

The importance here is to only have the numbers to be relative.

Note: Get-WMIobject is not preferred in this setting unfortunately.


Solution

  • -like is for wildcards. The simplest would be:

    Where-Object DisplayName -like 'Application 20* - English' 
    

    Or in regex:

    Where-Object DisplayName -match 'Application 20.* - English'   
    Where-Object DisplayName -match 'Application 20.. - English'   
    

    You could try -path with get-childitem: (-filter doesn't support the square brackets)

    get-childitem -path 'Application 20[1-2][07-9] - English'   
    get-childitem -path 'Application 20* - English'   
    get-childitem -path 'Application 20?? - English'   
    get-childitem -filter 'Application 20* - English'   
    get-childitem -filter 'Application 20?? - English'   
    

    -filter may be faster, but also matches the short version of the filenames.