Get-ChildItem with regular expression

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

  • Author
    Posts
  • #29814

    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

    Dilip Bailwal
    Participant

    you can use,

    | select-string -pattern "your regex"

  • #29817

    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

    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

    Dan Potter
    Participant

    All the variables are unneccesary.

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

    Simplified syntax kills puppies;-)

  • #29832

    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.