Get-ChildItem with regular expression

Welcome Forums General PowerShell Q&A Get-ChildItem with regular expression

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
3 years, 5 months ago.

  • Author
    Posts
  • #29814

    Participant
    Points: 1
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    you can use,

    | select-string -pattern "your regex"

  • #29817

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 1
    Rank: Member

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

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

  • #29830

    Participant
    Points: 21
    Rank: Member

    All the variables are unneccesary.

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

    Simplified syntax kills puppies;-)

  • #29832

    Participant
    Points: 1
    Rank: Member

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

The topic ‘Get-ChildItem with regular expression’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort