Get-ChildItem with regular expression

This topic contains 5 replies, has 4 voices, and was last updated by  Jason Wasser 2 years, 10 months ago.

  • Author
  • #29814

    Jason Wasser

    I'd like to find get a list of files within a certain directory but I need to include two different extension types (particularly *.cer and *.crt).

    Does the Get-ChildItem -Filter accept regular expressions?

    If not how else can I do what I need to do?

  • #29815

    Dilip Bailwal

    you can use,

    | select-string -pattern "your regex"

  • #29817

    Chris Wolfenden

    The filter parameter only qualifies the Path parameter so it wouldn't help you filter by file name. You can try piping to where-object

    Get-ChildItem ..... | Where-Object { {$_.Name -match "regex"}

  • #29821

    Jason Wasser

    Thank you. Here's what I came up with.

    $CertificateFileRegEx = '.cer|.crt'
    $Certifcates = Get-ChildItem -Path $CertificatePath | Where-Object -FilterScript {$_.Name -match $CertificateFileRegEx}

  • #29830

    Dan Potter

    All the variables are unneccesary.

    gci | ? -FilterScript {$ -match "csv|bin"}

    Simplified syntax kills puppies;-)

  • #29832

    Jason Wasser

    Thanks Dan. I'm putting together an advanced function which is why I'm being so verbose and using variables.

You must be logged in to reply to this topic.