Using PowerShell Regular Expressions to match against filenames

A short example for PowerShell Regular Expressions. Scenario: Match filenames like ID1234_MyDocumentXYZ.pdf.lnk. We want go get the number after ID and the rest of the filename between the underscore _ and the file extension .lnk. 1 2 3 4 5 $oldLink = "ID1337_MyDocumentXYZ.pdf.lnk" if($oldLink -match 'ID(?<id>\d+)_(?<actualFilename>.+)\.lnk$') { Write-Output "ID: $($Matches.id)" Write-Output "Actual Filename: $($Matches.actualFilename)" } (?<id>\d+) is a named regex capture group (initialized by ?<groupname>). The group matches any numeric character (\d)....

2021-10-25 · 1 min · Andreas Dieckmann