I'm glad to see that it works. I don't have an AD so I wasn't able to test the code.
If your intention is to just extract the first consecutive sequence of five digits that appear in a string that doesn't begin with a zero then you can eliminate almost all of your code with the correct regex. It would have been a lot easier to test if you'd posted a text file containing the data instead of an image!
$x = '00000000.00012345.bb.t',
'0000.12345.bb.t',
'000.12345.bb.t',
'00.12345.bb.t',
'00054321.00000000.t.b',
'00054321.00000000.t1.b1',
'00054321.00012345.t1.b1',
'54321.00012345.t1.b1',
'00000000.00067890.d.b',
'00000000.067890.d.b',
'00000000.67890.d.b',
'00000000.00067891.d.bb',
'00001234.1234.x.y',
'000A1234.A1234.x.y',
'000A1234.012345.x.y'
$x |
ForEach-Object{
if ($_ -match "([1-9]\d\d\d\d)"){
"{0} found in {1}" -f $matches[0], $_
}
else{
"Pattern not found in {0}" -f $_
}
}
Here are the results of running that test. Note that there are a couple of strings that won't match you criteria:
12345 found in 00000000.00012345.bb.t
12345 found in 0000.12345.bb.t
12345 found in 000.12345.bb.t
12345 found in 00.12345.bb.t
54321 found in 00054321.00000000.t.b
54321 found in 00054321.00000000.t1.b1
54321 found in 00054321.00012345.t1.b1
54321 found in 54321.00012345.t1.b1
67890 found in 00000000.00067890.d.b
67890 found in 00000000.067890.d.b
67890 found in 00000000.67890.d.b
67891 found in 00000000.00067891.d.bb
Pattern not found in 00001234.1234.x.y
Pattern not found in 000A1234.A1234.x.y
12345 found in 000A1234.012345.x.y