Get-ChildItem with regular expression

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Jason Wasser Jason Wasser 1 year, 10 months ago.

  • Author
    Posts
  • #29814
    Profile photo of Jason Wasser
    Jason Wasser
    Participant

    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
    Profile photo of Dilip Bailwal
    Dilip Bailwal
    Participant

    you can use,

    | select-string -pattern "your regex"

  • #29817
    Profile photo of Chris Wolfenden
    Chris Wolfenden
    Participant

    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
    Profile photo of Jason Wasser
    Jason Wasser
    Participant

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

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

  • #29830
    Profile photo of Dan Potter
    Dan Potter
    Participant

    All the variables are unneccesary.

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

    Simplified syntax kills puppies;-)

  • #29832
    Profile photo of Jason Wasser
    Jason Wasser
    Participant

    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.