Take the 2-minute tour ×

Using PowerShell, how can I get a list of file names within a string, if I only know the extension?

Simplified example:

C:\> $e = "CN=Ken Myer world.jpg more text."
C:\> $f = $e.IndexOf(".jpg")
C:\> write-host $f

How do I get the rest of the unknown file name? (Assuming a 5 character file name) I can not plug in a negative number into $e.Substring(17,-5,9) <--does not work.

share|improve this question
RegEx will help here, but you need to figure out a recurring pattern to match. For example, the file names can have spaces but are all terminated by ".jpg" obviously. If they all have "=" before them, you can construct a suitable RegEx easily. See the following pages for help: 1, 2, 3. –  Karan Oct 21 '12 at 14:18

1 Answer 1

$regex = '\b[A-Za-z0-9._%-+]+.jpg'

select-string -Path $DL_file -Pattern $regex -AllMatches | % { $.Matches } | % { $.Value } > $CurrentFileNames

Seems to work for my situation.


share|improve this answer

Your Answer


