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). The + afterwards is a multiplier, so we match against any amount of numeric characters. Since the Regular Expression matches successfully, the result is $true and we invoke the if section.

Output:

1
2
ID: 1337
Actual Filename: MyDocumentXYZ.pdf